English subtitles

← Testing Read All - Software Testing

Get Embed Code
1 Language

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Here's my read all function--this is the code I just took a few minutes to write,
  2. and what you can see is that I've included a couple of assertions.
  3. The first thing that I've asserted that the file descriptor passed 10 is greater than or equal to 0.
  4. These file descriptors are integers usually small ones and they have to be non-negative.
  5. The second thing we're going to assert is that buf that is to say pointing to the block of memory
  6. into which we're going to read data can't be the null pointer and just asserting buf like this
  7. is implicitly a test that the pointers not null in C.
  8. And the third thing we're going to assert is that the number of bytes to read is also non-negative.
  9. So, given that setup and we're just going little sanity checking here, we can get to the main logic.
  10. So the first thing we're going to do is initialize a left variable which is going to be the number of bytes
  11. left to read to initially be the total number of bytes to read.
  12. Now we're going to enter our while loop--the while loop is going to operate until either
  13. the read system call returns something less than 1 that is to say it returns 0
  14. indicating an end of file condition or -1 indicating an error.
  15. And if either of those things happens, we're just going to return that value to our caller.
  16. If read work that is to say it read some positive number of bytes, we're going to increment
  17. the buffer pointer by that number of bytes, reduce the amount of bytes left by that number of bytes,
  18. assert the number of bytes left hasn't gone negative due to some sort of a logic error,
  19. and now finally if there are no bytes left, we're going to return the number of bytes read
  20. which has to be just the original number of bytes but these asked to read.
  21. So this is pretty simple code and I would expect or at least I would hope to be able to get it right
  22. but I know from vast prior experience that I never get this stuffed right the first time,
  23. so what I want to do is be able to test it.
  24. So what I've done is written a little test runners, and so what the test runners does is opens a program
  25. and so here we're just using a splay tree from Python that happens to be in this directory
  26. and allocating a buffer and we don't need to worry about the details here
  27. and then what we're going to do is 100 different times we're going to issue the read all command
  28. to read the contents of the file into the buffer and then we're going to assert that
  29. read all--always read the full number of bytes we're trying to read and then
  30. we're also going to assert using a memory comparison function that
  31. the contents of that file are the contents that we expected.
  32. Its definitive variables initializes some code I didn't really talked about
  33. to contain the data from the file.
  34. And so what you're going to say if we do this 100 times, then our new read all call passes.
  35. And so let's look at what happens.