1
0

FileReader_test.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "io/Reader.h"
  17. #include "io/FileReader.h"
  18. #include "memory/Allocator.h"
  19. #include "memory/MallocAllocator.h"
  20. #include "util/Assert.h"
  21. #include "util/Bits.h"
  22. #include <stdio.h>
  23. #include <stdint.h>
  24. #include <stdbool.h>
  25. int main()
  26. {
  27. struct Allocator* alloc = MallocAllocator_new(1024);
  28. struct Random* rand = Random_new(alloc, NULL, NULL);
  29. FILE* tmp = tmpfile();
  30. uint8_t buffer1[2048];
  31. Random_bytes(rand, buffer1, 2048);
  32. fwrite(buffer1, 1, 2048, tmp);
  33. uint8_t buffer2[1024];
  34. rewind(tmp);
  35. struct Reader* r = FileReader_new(tmp, alloc);
  36. Reader_read(r, buffer2, 128);
  37. Reader_skip(r, 128);
  38. Reader_read(r, buffer2+128, 128);
  39. Reader_skip(r, 512);
  40. Reader_read(r, buffer2+128+128, 256);
  41. Reader_skip(r, 300);
  42. Reader_read(r, buffer2+128+128+256, 128);
  43. Assert_true(r->bytesRead == 128+128+128+512+256+300+128);
  44. uint8_t* ptr1 = buffer1;
  45. uint8_t* ptr2 = buffer2;
  46. #define SKIP(x) ptr1 += x
  47. #define CMP(x) Assert_true(!Bits_memcmp(ptr1, ptr2, x)); ptr1 += x; ptr2 += x
  48. CMP(128);
  49. SKIP(128);
  50. CMP(128);
  51. SKIP(512);
  52. CMP(256);
  53. SKIP(300);
  54. CMP(128);
  55. Allocator_free(alloc);
  56. return 0;
  57. }