0

I have a legacy C function which uses a struct definition as follows

struct{
    float _schedulerRate;
}sched_;

#define sched_1 sched_;


// bla bla bla

myFunc(bla bla bla) {

      // Lots of bla bla bla
      someVar = some_complicated_equation/sched_1._schedulerRate;
      // More bla bla bla
}

This function does not even accept any argument that somehow gets assigned to the sched_1._schedulerRate element. I also know that I cannot define the struct using extern because this linkage does support sturct definitions.

The problem is that when I am unit testing it, I have to set a value for this sched_1._scheduerRate. How can I do this? This is a legacy code that runs successfuly but I am trying to integrate one of its modules for a side project.

I have only 1.5 years experience in C/C++ programming and may be that's why I don't know some magic tricks. But even from a novice user's PoV, this looks almost impossible for me to do. However, it is legacy code and works too! how I don't know.

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • Since `sched_` is global, if you wanted to pull that function out and just test it, you can define your own `sched_1` value (set `sched_1.schedulerRate = ...;` and then call the function. (The `#define` is still in your post ;)). – lurker Oct 09 '13 at 14:49
  • sorry the define is in correct place. i just swapped sched_1 with sched_ as it was wrong before. – ha9u63a7 Oct 09 '13 at 15:00
  • Ah I see, makes sense, and I wasn't looking at it correctly. Then per my comment, same idea. `sched_` is scoped over the whole file. If your unit testing uses that file, then you'd set whatever values you need right in `sched_`. If you are pulling the function out into a separate file, you'd need to keep the define, or just directly define the variable `sched_1` to be of that `struct` type and set the values you need. – lurker Oct 09 '13 at 15:11
  • @mbratch I am not really sure how to do this anymore! I simply want to test this function in a `main()` file and yes, the function will be in a different file. I simply add the header file that has the `extern void myFunc()` declaration of the function. However, that header file DOESN'T have any `extern` declaration for the struct (is'nt possible to declare `extern struct` anyway). If I declare the struct in the `main()` file somewhere it still does not get the values from anywhere. Perhaps, if you are kind to put at as your answer exactly what you mean, I can understand better! Thanks – ha9u63a7 Oct 10 '13 at 06:30

0 Answers0