~ubuntu-branches/ubuntu/precise/protobuf/precise

« back to all changes in this revision

Viewing changes to src/google/protobuf/repeated_field_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2009-11-16 10:41:33 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20091116104133-ykhy3deg5l4975tw
Tags: 2.1.0-1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Disable the death tests on IA64, now as a quilt patch.
  - Don't use python2.4, also as a quilt patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
//   other proto2 unittests.
37
37
 
38
38
#include <algorithm>
 
39
#include <list>
39
40
 
40
41
#include <google/protobuf/repeated_field.h>
41
42
 
42
43
#include <google/protobuf/stubs/common.h>
 
44
#include <google/protobuf/unittest.pb.h>
 
45
#include <google/protobuf/stubs/strutil.h>
43
46
#include <google/protobuf/testing/googletest.h>
44
47
#include <gtest/gtest.h>
 
48
#include <google/protobuf/stubs/stl_util-inl.h>
45
49
 
46
50
namespace google {
 
51
using protobuf_unittest::TestAllTypes;
 
52
 
47
53
namespace protobuf {
 
54
namespace {
48
55
 
49
56
// Test operations on a RepeatedField which is small enough that it does
50
57
// not allocate a separate array for storage.
621
628
  EXPECT_EQ("qux", proto_array_.Get(0));
622
629
}
623
630
 
 
631
// -----------------------------------------------------------------------------
 
632
// Unit-tests for the insert iterators
 
633
// google::protobuf::RepeatedFieldBackInserter,
 
634
// google::protobuf::AllocatedRepeatedPtrFieldBackInserter
 
635
// Ported from util/gtl/proto-array-iterators_unittest.
 
636
 
 
637
class RepeatedFieldInsertionIteratorsTest : public testing::Test {
 
638
 protected:
 
639
  std::list<double> halves;
 
640
  std::list<int> fibonacci;
 
641
  std::vector<string> words;
 
642
  typedef TestAllTypes::NestedMessage Nested;
 
643
  Nested nesteds[2];
 
644
  std::vector<Nested*> nested_ptrs;
 
645
  TestAllTypes protobuffer;
 
646
 
 
647
  virtual void SetUp() {
 
648
    fibonacci.push_back(1);
 
649
    fibonacci.push_back(1);
 
650
    fibonacci.push_back(2);
 
651
    fibonacci.push_back(3);
 
652
    fibonacci.push_back(5);
 
653
    fibonacci.push_back(8);
 
654
    std::copy(fibonacci.begin(), fibonacci.end(),
 
655
              RepeatedFieldBackInserter(protobuffer.mutable_repeated_int32()));
 
656
 
 
657
    halves.push_back(1.0);
 
658
    halves.push_back(0.5);
 
659
    halves.push_back(0.25);
 
660
    halves.push_back(0.125);
 
661
    halves.push_back(0.0625);
 
662
    std::copy(halves.begin(), halves.end(),
 
663
              RepeatedFieldBackInserter(protobuffer.mutable_repeated_double()));
 
664
 
 
665
    words.push_back("Able");
 
666
    words.push_back("was");
 
667
    words.push_back("I");
 
668
    words.push_back("ere");
 
669
    words.push_back("I");
 
670
    words.push_back("saw");
 
671
    words.push_back("Elba");
 
672
    std::copy(words.begin(), words.end(),
 
673
              RepeatedFieldBackInserter(protobuffer.mutable_repeated_string()));
 
674
 
 
675
    nesteds[0].set_bb(17);
 
676
    nesteds[1].set_bb(4711);
 
677
    std::copy(&nesteds[0], &nesteds[2],
 
678
              RepeatedFieldBackInserter(
 
679
                  protobuffer.mutable_repeated_nested_message()));
 
680
 
 
681
    nested_ptrs.push_back(new Nested);
 
682
    nested_ptrs.back()->set_bb(170);
 
683
    nested_ptrs.push_back(new Nested);
 
684
    nested_ptrs.back()->set_bb(47110);
 
685
    std::copy(nested_ptrs.begin(), nested_ptrs.end(),
 
686
              RepeatedFieldBackInserter(
 
687
                  protobuffer.mutable_repeated_nested_message()));
 
688
 
 
689
  }
 
690
 
 
691
  virtual void TearDown() {
 
692
    STLDeleteContainerPointers(nested_ptrs.begin(), nested_ptrs.end());
 
693
  }
 
694
};
 
695
 
 
696
TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) {
 
697
  EXPECT_TRUE(std::equal(fibonacci.begin(),
 
698
                         fibonacci.end(),
 
699
                         protobuffer.repeated_int32().begin()));
 
700
  EXPECT_TRUE(std::equal(protobuffer.repeated_int32().begin(),
 
701
                         protobuffer.repeated_int32().end(),
 
702
                         fibonacci.begin()));
 
703
}
 
704
 
 
705
TEST_F(RepeatedFieldInsertionIteratorsTest, Halves) {
 
706
  EXPECT_TRUE(std::equal(halves.begin(),
 
707
                         halves.end(),
 
708
                         protobuffer.repeated_double().begin()));
 
709
  EXPECT_TRUE(std::equal(protobuffer.repeated_double().begin(),
 
710
                         protobuffer.repeated_double().end(),
 
711
                         halves.begin()));
 
712
}
 
713
 
 
714
TEST_F(RepeatedFieldInsertionIteratorsTest, Words) {
 
715
  ASSERT_EQ(words.size(), protobuffer.repeated_string_size());
 
716
  EXPECT_EQ(words.at(0), protobuffer.repeated_string(0));
 
717
  EXPECT_EQ(words.at(1), protobuffer.repeated_string(1));
 
718
  EXPECT_EQ(words.at(2), protobuffer.repeated_string(2));
 
719
  EXPECT_EQ(words.at(3), protobuffer.repeated_string(3));
 
720
  EXPECT_EQ(words.at(4), protobuffer.repeated_string(4));
 
721
  EXPECT_EQ(words.at(5), protobuffer.repeated_string(5));
 
722
  EXPECT_EQ(words.at(6), protobuffer.repeated_string(6));
 
723
}
 
724
 
 
725
TEST_F(RepeatedFieldInsertionIteratorsTest, Nesteds) {
 
726
  ASSERT_EQ(protobuffer.repeated_nested_message_size(), 4);
 
727
  EXPECT_EQ(protobuffer.repeated_nested_message(0).bb(), 17);
 
728
  EXPECT_EQ(protobuffer.repeated_nested_message(1).bb(), 4711);
 
729
  EXPECT_EQ(protobuffer.repeated_nested_message(2).bb(), 170);
 
730
  EXPECT_EQ(protobuffer.repeated_nested_message(3).bb(), 47110);
 
731
}
 
732
 
 
733
TEST_F(RepeatedFieldInsertionIteratorsTest,
 
734
       AllocatedRepeatedPtrFieldWithStringIntData) {
 
735
  vector<Nested*> data;
 
736
  TestAllTypes goldenproto;
 
737
  for (int i = 0; i < 10; ++i) {
 
738
    Nested* new_data = new Nested;
 
739
    new_data->set_bb(i);
 
740
    data.push_back(new_data);
 
741
 
 
742
    new_data = goldenproto.add_repeated_nested_message();
 
743
    new_data->set_bb(i);
 
744
  }
 
745
  TestAllTypes testproto;
 
746
  copy(data.begin(), data.end(),
 
747
       AllocatedRepeatedPtrFieldBackInserter(
 
748
           testproto.mutable_repeated_nested_message()));
 
749
  EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
 
750
}
 
751
 
 
752
TEST_F(RepeatedFieldInsertionIteratorsTest,
 
753
       AllocatedRepeatedPtrFieldWithString) {
 
754
  vector<string*> data;
 
755
  TestAllTypes goldenproto;
 
756
  for (int i = 0; i < 10; ++i) {
 
757
    string* new_data = new string;
 
758
    *new_data = "name-" + SimpleItoa(i);
 
759
    data.push_back(new_data);
 
760
 
 
761
    new_data = goldenproto.add_repeated_string();
 
762
    *new_data = "name-" + SimpleItoa(i);
 
763
  }
 
764
  TestAllTypes testproto;
 
765
  copy(data.begin(), data.end(),
 
766
       AllocatedRepeatedPtrFieldBackInserter(
 
767
           testproto.mutable_repeated_string()));
 
768
  EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
 
769
}
 
770
 
 
771
}  // namespace
 
772
 
624
773
}  // namespace protobuf
625
774
}  // namespace google