0

I am trying to make a main function which will test a whole bunch of other functions to make sure that they work. That is, if they contain bugs, return 1, if no bugs, return 0

Suppose one of the functions I am trying to test is the following:

void list_print(list_t*list);

this should print a list in "human-readable form" from first to last elements, between curly braces.

Now, naturally I could simply print an array and see for myself that it looks good, but I need to do this so that it is automatic, i.e. if the print function prints accurately, return 0, otherwise, return 1.

How do I go about doing this (in a general sort of way)? I keep googling and can't really find the answer.

phuclv
  • 37,963
  • 15
  • 156
  • 475
whoshotya
  • 49
  • 4
  • What have you tried to accomplish wrt `list_print`? Show us the code you have written ? – Rishi Mar 29 '17 at 05:32
  • Do you want to check the elements in `list` whether they will print in human readable form? – Rishikesh Raje Mar 29 '17 at 05:33
  • "i.e. if the print function prints accurately, return 0, otherwise, return 1." What does **prints accurately** mean?! I suggest to provide an example. This is really vague. – Arash Mar 29 '17 at 05:34
  • 3
    I don't know if I understand your question. Do you want to test the printing function and see if it outputs what you expect it to output? If so, then you wouldn't really be able to "see" the terminal output, but you could redirect `stdout` to a buffer in memory using `freopen` and `setbuf` and compare it with what you expect the output to be. – Greg Schmit Mar 29 '17 at 05:34

2 Answers2

1

I recommend that you redirect stdout to a buffer in memory using freopen and setbuf, as they describe in this question. Then, run the list_print method.

Then you can compare the contents of your buffer with what you would expect to see. You can be strict on this step or you can use heuristics to try to identify a generally accepted pattern of output that is acceptable.

I don't think there is a better way to "see" what is output to the terminal, except for un-portable OS-specific libraries.

If you post your working attempt then I can help you out more with a concrete code example.

Community
  • 1
  • 1
Greg Schmit
  • 4,275
  • 2
  • 21
  • 36
1

As a general answer to this sort of testing problem: you run it once on a given, fixed, input and you inspect the result. If it 'looks right' then you write a test that provides exactly the same input every time and blindly compares the output with the expected output, which is the output you 'manually' verified and it 'looks right'. This will prevent a regression in the function implementation.

You can go further and add a number of test cases (empty list, one entry in list, more entries etc) and then write a similar test for each case. These are 'expected result' tests, where the test does not know what the 'looks right' means, but it simply verifies that for a given input the output is the one 'expected' and it does not deviate. The outputs themselves are verified that they 'look right' by the person writing the test, which adds the 'expected output', manually, when the test is written and accepted.

The alternative is you have to write logic to parse the output, perhaps a full blown lexer and grammar, and truly validate the output. For this you need to very precisely define what output is 'right' and write an appropriate test parser for it. Is a worthy goal if the function is important enough, but this does not appear to be your case.

BTW, do not write a function that 'return 1 if the function has bugs', but instead use a test framework and add this as a build phase to your project.

Community
  • 1
  • 1
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569