sort_by_tag.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "sort_by_tag.pb.h"
  2. #include <pb_encode.h>
  3. #include <pb_decode.h>
  4. #include "unittests.h"
  5. int main()
  6. {
  7. int status = 0;
  8. size_t msglen;
  9. pb_byte_t buf[256];
  10. {
  11. pb_ostream_t ostream = pb_ostream_from_buffer(buf, sizeof(buf));
  12. Unsorted msg = Unsorted_init_zero;
  13. COMMENT("Test encoding with unsorted structure");
  14. TEST(&msg.first < &msg.oneof.second);
  15. TEST(&msg.oneof.second < &msg.last);
  16. msg.first = 101;
  17. msg.which_oneof = Unsorted_second_tag;
  18. msg.oneof.second = 102;
  19. msg.last = 103;
  20. if (!pb_encode(&ostream, Unsorted_fields, &msg))
  21. {
  22. fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&ostream));
  23. return 1;
  24. }
  25. msglen = ostream.bytes_written;
  26. }
  27. {
  28. pb_istream_t istream = pb_istream_from_buffer(buf, msglen);
  29. Sorted msg = Sorted_init_zero;
  30. COMMENT("Test decoding with sorted structure");
  31. if (!pb_decode(&istream, Sorted_fields, &msg))
  32. {
  33. fprintf(stderr, "Decoding failed: %s\n", PB_GET_ERROR(&istream));
  34. return 2;
  35. }
  36. TEST(msg.first == 101);
  37. TEST(msg.which_oneof == Sorted_second_tag);
  38. TEST(msg.oneof.second == 102);
  39. TEST(msg.last == 103);
  40. TEST(&msg.first > &msg.oneof.second);
  41. TEST(&msg.oneof.second > &msg.last);
  42. }
  43. return status;
  44. }