iterators_ut.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "iterators.h"
  2. #include "simple_reflection.h"
  3. #include <library/cpp/protobuf/util/ut/common_ut.pb.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. #include <util/generic/algorithm.h>
  6. using NProtoBuf::TFieldsIterator;
  7. using NProtoBuf::TConstField;
  8. Y_UNIT_TEST_SUITE(Iterators) {
  9. Y_UNIT_TEST(Count) {
  10. const NProtobufUtilUt::TWalkTest proto;
  11. const NProtoBuf::Descriptor* d = proto.GetDescriptor();
  12. TFieldsIterator dbegin(d), dend(d, d->field_count());
  13. size_t steps = 0;
  14. UNIT_ASSERT_EQUAL(dbegin, begin(*d));
  15. UNIT_ASSERT_EQUAL(dend, end(*d));
  16. for (; dbegin != dend; ++dbegin)
  17. ++steps;
  18. UNIT_ASSERT_VALUES_EQUAL(steps, d->field_count());
  19. }
  20. Y_UNIT_TEST(RangeFor) {
  21. size_t steps = 0, values = 0;
  22. NProtobufUtilUt::TWalkTest proto;
  23. proto.SetOptStr("yandex");
  24. for (const auto& field : *proto.GetDescriptor()) {
  25. values += TConstField(proto, field).HasValue();
  26. ++steps;
  27. }
  28. UNIT_ASSERT_VALUES_EQUAL(steps, proto.GetDescriptor()->field_count());
  29. UNIT_ASSERT_VALUES_EQUAL(values, 1);
  30. }
  31. Y_UNIT_TEST(AnyOf) {
  32. NProtobufUtilUt::TWalkTest proto;
  33. const NProtoBuf::Descriptor* d = proto.GetDescriptor();
  34. TFieldsIterator begin(d), end(d, d->field_count());
  35. UNIT_ASSERT(!AnyOf(begin, end, [&proto](const NProtoBuf::FieldDescriptor* f){
  36. return TConstField(proto, f).HasValue();
  37. }));
  38. proto.SetOptStr("yandex");
  39. UNIT_ASSERT(AnyOf(begin, end, [&proto](const NProtoBuf::FieldDescriptor* f){
  40. return TConstField(proto, f).HasValue();
  41. }));
  42. }
  43. }