-3

I've written a C code which takes a path to a directory and a string s. After opening the directory, the code looks for files with a particular extension and write the string given as a second argument to the last line of these files and a \n after it. This is all my function does (finding files with a particular extension and writing something at the end of the file). The C code for the function is done but I need to write some tests for the function using Cunit and I don't have any Idea how to write them. I've just written down what I want to test (below) but I don't know how to write them using Cunit. Can anyone help me with that? Thanks

  • check if the user checks if the string 's' is nULL => segmentation fault
  • check if bad address is given to the path argument of the function when there is no ".extension" file
  • check if strcmp compares ".extension" and not "extension"
  • check if the user uses == instead of strcmp to compare the extension
  • check if the string 's' is written on a new line
  • check the size of the string 's' sent to write => sent without \0 by strlen and the type is size_t
  • check if the file is opened for writing (and not read only)
  • check if string 's' is converted to void* before giving it to write function
  • check if the return value of open ("fd") is checked
  • check if the return value of opening a directory is checked (null or not)
  • check if the return value of reading a directory is checked (null or not)
  • check if the extension is checked (the string 's' is not added to all the files with other extensions
  • check if the function ignores the '.' et '..' directories => DT_REG
  • check if the file is closed after being opened
  • check the return value of close
  • check if the directory is closed after being opened
  • check the return value of the directory
jamal
  • 543
  • 1
  • 4
  • 5
  • _There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs._ – Sourav Ghosh May 13 '16 at 06:42
  • You need to refer a tutorial, IMHO. – Sourav Ghosh May 13 '16 at 06:42
  • @Sourav Ghosh I don't know why some people don't want to answer the questions properly. They just give you a negative point saying this question was blablabla and there U go !!!! instead U can give me the tutorial website or something like this – jamal May 13 '16 at 06:56
  • Please show your research effort till time. Please read [Ask] page first. – Sourav Ghosh May 13 '16 at 06:57
  • And moreover, I think you're the one asking for help here, so first learn to be polite. No offense. :) – Sourav Ghosh May 13 '16 at 06:57
  • When I ask my question here, that means I haven't found anything helpful on the internet – jamal May 13 '16 at 06:59
  • So you mean, you access SO without internet? :D – Sourav Ghosh May 13 '16 at 07:00
  • I mean I've already searched for helpful tutorials etc. but didn't find any. So, I decided to come in this website and ask – jamal May 13 '16 at 07:02
  • Picking one of your tests not entirely at random: "check if the return value of opening a directory is checked (null or not)". This isn't a good unit test, in my view. It requires source code inspection. You can "check whether the code handles a non-existent directory, a non-directory (file, socket, FIFO, etc), or an inaccessible directory" by pointing the code at such problematic names, and ensuring it does work sanely (doesn't crash because of the null pointer returned by `opendir()`, for example — and is designed so it has a way to report its problems). Those are testable conditions. – Jonathan Leffler May 13 '16 at 07:52
  • Another of your tests is 'check if strcmp compares ".extension" and not "extension"'. This again is badly worded, which makes it hard to test (or it is pointless to test — `strcmp()` tests what you tell it to test and is equally happy with either). You probably should be testing: 'code does not modify a file named "filename-extension" but does modify a file named "filename.extension"'. That is testable in a way which the original is not. – Jonathan Leffler May 13 '16 at 07:57

1 Answers1

2

Unit tests are a kind of driver functions (i.e. like main() )for your code i.e. consider you have written a function foo. In foo you have to handle each thing/case properly.

Once you are done with it, you need to verify it with a Unit test which will pass different arguments to foo and let you know if every thing working properly or not.

check if the user checks if the string 's' is nULL => segmentation fault

check if bad address is given to the path argument of the function when there is no ".extension" file

check if strcmp compares ".extension" and not "extension"

etc. this kind of checks are supposed to be written in your function foo

Consider the example.

int divide(int numerator, int denominator) {

if (denominator == 0)
      return -1; 
else 
      return numerator/denominator;
}

In code you have done checking yourself. Now you would have to verify it with some Unit test framework or any other driver function like

void main(void) {

/* Pass correct arguments and expect it will return positive */ 
assert(divide(10, 2) > -1);
printf("Test passed for correct arguments\n");


/* Pass wrong arguments and expect it will return -1 */ 
assert(divide(10,0) == -1); 
printf("Test passes for wrong arguments\n);

}

In failure of your expected values, test would fail and your divide function is not correct. Hope this will help you.

Mazhar
  • 575
  • 5
  • 20
  • Yeah I know. But in here, what confuses me, is that I have to check the result in a file (to see if the string appears in files with a particular extension). Do I have to check that by reading the files ? – jamal May 13 '16 at 06:53
  • 2
    @jamal: If you need to check whether a file ends with a given string, how are you possibly going to do it _without_ reading the file? In other words, yes — you do have to check whether the required string appears at the end of the file by opening and reading (and closing) the file after the C code has dealt with the addition. – Jonathan Leffler May 13 '16 at 07:07
  • Yes of course bro, a very good explanation by @JonathanLeffler – Mazhar May 13 '16 at 07:09
  • Thanks JonathanLeffler and Mazhar. @Mazhar I've checked all the tests I've written above, in my function. But I don't know how to test them with Unit Testing. My function does not return value. How do I use assert to see if I pass Null to one of its arguments, it will not work ? – jamal May 13 '16 at 07:23
  • Then you should define some error codes using #define and return them from your function. This is good practice. Without returning something how come you know that something went wrong. So even if your code shouldn't have to return some thing, you should return status of the code. in case of error return -1 and success return 0 etc. | And then check in your code if the input string is null then return -1 etc. and at the end of the function if every thing went successful, return 0. – Mazhar May 13 '16 at 07:51