1
0

test_base16.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include "types.h"
  4. #include "base16.h"
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <stdio.h>
  8. /*
  9. BASE16("") = ""
  10. BASE16("f") = "66"
  11. BASE16("fo") = "666F"
  12. BASE16("foo") = "666F6F"
  13. BASE16("foob") = "666F6F62"
  14. BASE16("fooba") = "666F6F6261"
  15. BASE16("foobar") = "666F6F626172"
  16. */
  17. struct texttestcase {
  18. const char *in;
  19. const char *out;
  20. const char *rev;
  21. } tests0[] = {
  22. {"", "", ""},
  23. {"f", "66", "f"},
  24. {"fo", "666F", "fo"},
  25. {"foo", "666F6F", "foo"},
  26. {"foob", "666F6F62", "foob"},
  27. {"fooba", "666F6F6261", "fooba"},
  28. {"foobar", "666F6F626172", "foobar"},
  29. };
  30. int main(void)
  31. {
  32. char buf[1024], buf2[1024], mask;
  33. size_t r;
  34. for (size_t i = 0; i < sizeof(tests0)/sizeof(tests0[0]); ++i) {
  35. base16_to(buf, (const u8 *)tests0[i].in, strlen(tests0[i].in));
  36. assert(strcmp(buf, tests0[i].out) == 0);
  37. r = base16_from((u8 *)buf2, (u8 *)&mask, buf);
  38. buf2[r] = 0;
  39. //fprintf(stderr, "r:%d, mask:%02X\n", (int)r, ((unsigned int)mask) & 0xFF);
  40. //assert(r == strlen(buf2));
  41. //assert(r == strlen(tests0[i].rev));
  42. //fprintf(stderr, "%s -- %s\n", buf2, tests0[i].rev);
  43. assert(strcmp(buf2, tests0[i].rev) == 0);
  44. }
  45. return 0;
  46. }