test_byte.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdio.h>
  2. #include "aFun.h"
  3. #include "tool.h"
  4. #define TEST_WRITE(test, type) do{\
  5. if (!(test)) { \
  6. fprintf(stderr, "Test write wrong: " #type "\n"); \
  7. return EXIT_FAILURE; \
  8. }}while(0)
  9. #define TEST_READ(test, type) do{\
  10. if (!(test)) { \
  11. fprintf(stderr, "Test read wrong: " #type "\n"); \
  12. return EXIT_FAILURE; \
  13. }}while(0)
  14. int main() {
  15. int8_t test8 = 10;
  16. int16_t test16 = 20;
  17. int32_t test32 = 30;
  18. int64_t test64 = 40;
  19. char *testStr = "test";
  20. getEndian();
  21. FILE *file = fopen("test.byte", "wb");
  22. if (file == NULL) {
  23. fprintf(stderr, "Can't not creat file: test.byte\n");
  24. return EXIT_FAILURE;
  25. }
  26. TEST_WRITE(byteWriteInt_8(file, test8), uint8_t);
  27. TEST_WRITE(byteWriteInt_16(file, test16), uint16_t);
  28. TEST_WRITE(byteWriteInt_32(file, test32), uint32_t);
  29. TEST_WRITE(byteWriteInt_64(file, test64), uint64_t);
  30. TEST_WRITE(byteWriteStr(file, testStr), str);
  31. fclose(file);
  32. int8_t rtest8;
  33. int16_t rtest16;
  34. int32_t rtest32;
  35. int64_t rtest64;
  36. char *rtestStr;
  37. file = fopen("test.byte", "rb");
  38. if (file == NULL) {
  39. fprintf(stderr, "Can't not read file: test.byte\n");
  40. return EXIT_FAILURE;
  41. }
  42. TEST_READ(byteReadInt_8(file, &rtest8), uint8_t);
  43. TEST_READ(byteReadInt_16(file, &rtest16), uint16_t);
  44. TEST_READ(byteReadInt_32(file, &rtest32), uint32_t);
  45. TEST_READ(byteReadInt_64(file, &rtest64), uint64_t);
  46. TEST_READ(byteReadStr(file, &rtestStr), str);
  47. if (rtest8 != test8 || rtest16 != test16 || rtest32 != test32 || rtest64 != test64 || !EQ_STR(rtestStr, testStr)) {
  48. printf("error.\n");
  49. printf("rtest8 = %d , test = %d\n", rtest8, test8);
  50. printf("rtest16 = %d, test = %d\n", rtest16, test16);
  51. printf("rtest32 = %d, test = %d\n", rtest32, test32);
  52. printf("rtest64 = %ld, test = %ld\n", rtest64, test64);
  53. printf("rtestStr = %s\ntestStr = %s\n", rtestStr, testStr);
  54. return EXIT_FAILURE;
  55. }
  56. free(rtestStr);
  57. printf("success.\n");
  58. return EXIT_SUCCESS;
  59. }