1
0

FileReader_test.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "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. size_t checkSize;
  32. Random_bytes(rand, buffer1, 2048);
  33. checkSize = fwrite(buffer1, 1, 2048, tmp);
  34. if (checkSize != 2048)
  35. {
  36. return 1;
  37. }
  38. uint8_t buffer2[1024];
  39. rewind(tmp);
  40. struct Reader* r = FileReader_new(tmp, alloc);
  41. Reader_read(r, buffer2, 128);
  42. Reader_skip(r, 128);
  43. Reader_read(r, buffer2+128, 128);
  44. Reader_skip(r, 512);
  45. Reader_read(r, buffer2+128+128, 256);
  46. Reader_skip(r, 300);
  47. Reader_read(r, buffer2+128+128+256, 128);
  48. Assert_true(r->bytesRead == 128+128+128+512+256+300+128);
  49. uint8_t* ptr1 = buffer1;
  50. uint8_t* ptr2 = buffer2;
  51. #define SKIP(x) ptr1 += x
  52. #define CMP(x) Assert_true(!Bits_memcmp(ptr1, ptr2, x)); ptr1 += x; ptr2 += x
  53. CMP(128);
  54. SKIP(128);
  55. CMP(128);
  56. SKIP(512);
  57. CMP(256);
  58. SKIP(300);
  59. CMP(128);
  60. Allocator_free(alloc);
  61. return 0;
  62. }