inline_unittests.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <pb_decode.h>
  4. #include <pb_encode.h>
  5. #include "unittests.h"
  6. #include "inline.pb.h"
  7. int main()
  8. {
  9. int status = 0;
  10. int i = 0;
  11. COMMENT("Test inline byte fields");
  12. {
  13. Message1 msg1 = Message1_init_zero;
  14. TEST(sizeof(msg1.data) == 32);
  15. }
  16. {
  17. Message1 msg1 = Message1_init_zero;
  18. pb_byte_t msg1_buffer[Message1_size];
  19. pb_ostream_t ostream = pb_ostream_from_buffer(msg1_buffer, Message1_size);
  20. Message1 msg1_deserialized = Message1_init_zero;
  21. pb_istream_t istream = pb_istream_from_buffer(msg1_buffer, Message1_size);
  22. for (i = 0; i < 32; i++) {
  23. msg1.data[i] = i;
  24. }
  25. TEST(pb_encode(&ostream, Message1_fields, &msg1));
  26. TEST(ostream.bytes_written == Message1_size);
  27. TEST(pb_decode(&istream, Message1_fields, &msg1_deserialized));
  28. TEST(istream.bytes_left == 0);
  29. TEST(memcmp(&msg1_deserialized, &msg1, sizeof(msg1)) == 0);
  30. }
  31. {
  32. Message2 msg2 = {true, {0}};
  33. Message2 msg2_no_data = {false, {1}};
  34. pb_byte_t msg2_buffer[Message2_size];
  35. pb_ostream_t ostream = pb_ostream_from_buffer(msg2_buffer, Message2_size);
  36. Message2 msg2_deserialized = Message2_init_zero;
  37. pb_istream_t istream = pb_istream_from_buffer(msg2_buffer, Message2_size);
  38. for (i = 0; i < 64; i++) {
  39. msg2.data[i] = i;
  40. }
  41. TEST(pb_encode(&ostream, Message2_fields, &msg2));
  42. TEST(ostream.bytes_written == Message2_size);
  43. TEST(pb_decode(&istream, Message2_fields, &msg2_deserialized));
  44. TEST(istream.bytes_left == 0);
  45. TEST(memcmp(&msg2_deserialized, &msg2, sizeof(msg2)) == 0);
  46. TEST(msg2_deserialized.has_data);
  47. memset(msg2_buffer, 0, sizeof(msg2_buffer));
  48. ostream = pb_ostream_from_buffer(msg2_buffer, Message2_size);
  49. TEST(pb_encode(&ostream, Message2_fields, &msg2_no_data));
  50. istream = pb_istream_from_buffer(msg2_buffer, ostream.bytes_written);
  51. TEST(pb_decode(&istream, Message2_fields, &msg2_deserialized));
  52. TEST(!msg2_deserialized.has_data);
  53. TEST(memcmp(&msg2_deserialized, &msg2, sizeof(msg2)) != 0);
  54. }
  55. if (status != 0)
  56. fprintf(stdout, "\n\nSome tests FAILED!\n");
  57. return status;
  58. }