FileReader_test.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <https://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 "util/Assert.h"
  20. #include "util/Bits.h"
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. int main()
  25. {
  26. struct Allocator* alloc = Allocator_new(2048);
  27. struct Random* rand = Random_new(alloc, NULL, NULL);
  28. FILE* tmp = tmpfile();
  29. uint8_t buffer1[2048];
  30. size_t checkSize;
  31. Random_bytes(rand, buffer1, 2048);
  32. checkSize = fwrite(buffer1, 1, 2048, tmp);
  33. if (checkSize != 2048)
  34. {
  35. return 1;
  36. }
  37. uint8_t buffer2[1024];
  38. rewind(tmp);
  39. struct Reader* r = FileReader_new(tmp, alloc);
  40. Reader_read(r, buffer2, 128);
  41. Reader_skip(r, 128);
  42. Reader_read(r, buffer2+128, 128);
  43. Reader_skip(r, 512);
  44. Reader_read(r, buffer2+128+128, 256);
  45. Reader_skip(r, 300);
  46. Reader_read(r, buffer2+128+128+256, 128);
  47. Assert_true(r->bytesRead == 128+128+128+512+256+300+128);
  48. uint8_t* ptr1 = buffer1;
  49. uint8_t* ptr2 = buffer2;
  50. #define SKIP(x) ptr1 += x
  51. #define CMP(x) Assert_true(!Bits_memcmp(ptr1, ptr2, x)); ptr1 += x; ptr2 += x
  52. CMP(128);
  53. SKIP(128);
  54. CMP(128);
  55. SKIP(512);
  56. CMP(256);
  57. SKIP(300);
  58. CMP(128);
  59. Allocator_free(alloc);
  60. return 0;
  61. }