mixed.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <pb_encode.h>
  2. #include <pb_decode.h>
  3. #include "mixed.pb.h"
  4. #include "unittests.h"
  5. int main()
  6. {
  7. int status = 0;
  8. pb_byte_t buf[64];
  9. size_t msglen;
  10. {
  11. pb_ostream_t ostream = pb_ostream_from_buffer(buf, sizeof(buf));
  12. MixedMessage msg = MixedMessage_init_default;
  13. msg.has_proto2_value = true;
  14. msg.proto2_value = 0;
  15. msg.proto3_value = 0;
  16. if (!pb_encode(&ostream, MixedMessage_fields, &msg))
  17. {
  18. fprintf(stderr, "pb_encode() failed: %s\n", PB_GET_ERROR(&ostream));
  19. return 1;
  20. }
  21. msglen = ostream.bytes_written;
  22. TEST(msglen > 0);
  23. }
  24. {
  25. pb_istream_t istream = pb_istream_from_buffer(buf, msglen);
  26. MixedMessage msg = MixedMessage_init_default;
  27. TEST(msg.proto2_value == 100);
  28. TEST(msg.proto3_value == 200);
  29. if (!pb_decode(&istream, MixedMessage_fields, &msg))
  30. {
  31. fprintf(stderr, "pb_decode() failed: %s\n", PB_GET_ERROR(&istream));
  32. return 1;
  33. }
  34. TEST(msg.proto2_value == 0);
  35. TEST(msg.proto3_value == 0);
  36. }
  37. return status;
  38. }