1 /* -*- c++ -*- */
2 /*
3 * Copyright (C) 2010 The Android Open Source Project
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include "../include/set"
31 #ifndef ANDROID_ASTL_SET__
32 #error "Wrong header included!!"
33 #endif
34 #include <climits>
35 #include <cstring>
36 #include <string>
37 #include "common.h"
38
39 namespace android {
40 using std::pair;
41 using std::set;
42 using std::string;
43
testConstructor()44 bool testConstructor()
45 {
46 set<int> s;
47 EXPECT_TRUE(s.empty());
48 EXPECT_TRUE(s.size() == 0);
49 EXPECT_TRUE(s.begin() == s.end());
50 EXPECT_TRUE(s.count(10) == 0);
51 return true;
52 }
53
testInsertPOD()54 bool testInsertPOD()
55 {
56 set<int> s;
57 pair<set<int>::iterator, bool> res;
58
59 EXPECT_TRUE(s.count(10) == 0);
60
61 res = s.insert(10);
62
63 // begin should point to the element inserted.
64 EXPECT_TRUE(res.first == s.begin());
65 EXPECT_TRUE(s.end() != s.begin());
66 EXPECT_TRUE(*(res.first) == 10);
67 set<int>::iterator elt_in_set = res.first;
68 EXPECT_TRUE(*(s.begin()) == 10);
69
70 // insert was a success.
71 EXPECT_TRUE(res.second);
72
73 // element can be found
74 EXPECT_TRUE(s.count(10) == 1);
75
76 // Try to insert the same element again, this time it should fail.
77 res = s.insert(10);
78 // insert was a failure.
79 EXPECT_TRUE(!res.second);
80
81 // Insert should return an iterator pointing to the element
82 // already in the set.
83 EXPECT_TRUE(res.first == elt_in_set);
84
85 // element can still be found
86 EXPECT_TRUE(s.count(10) == 1);
87 return true;
88 }
89
testInsertString()90 bool testInsertString()
91 {
92 set<string> s;
93 pair<set<string>::iterator, bool> res;
94 string str("a string");
95 string str_equiv("a string");
96 string str_missing("a string not in the set");
97
98 EXPECT_TRUE(s.count(str) == 0);
99
100 res = s.insert(str);
101
102 // begin should point to the element inserted.
103 EXPECT_TRUE(res.first == s.begin());
104 set<string>::iterator marker = res.first;
105 EXPECT_TRUE(s.end() != s.begin());
106 EXPECT_TRUE(*(res.first) == str);
107 EXPECT_TRUE(*(s.begin()) == str);
108
109 // insert was a success.
110 EXPECT_TRUE(res.second);
111
112 // element can be found
113 EXPECT_TRUE(s.count(str) == 1);
114
115 // Try to insert an element equivalent.
116 res = s.insert(str_equiv);
117
118 // insert did not happen since there is one string equivalent
119 // already.
120 EXPECT_TRUE(!res.second);
121
122 // The iterator points to the copy already in the set.
123 EXPECT_TRUE(res.first == marker);
124
125 // element can still be found
126 EXPECT_TRUE(s.count(str) == 1);
127 EXPECT_TRUE(s.count(str_equiv) == 1);
128 return true;
129 }
130
131 } // namespace android
132
main(int argc,char ** argv)133 int main(int argc, char **argv)
134 {
135 FAIL_UNLESS(testConstructor);
136 FAIL_UNLESS(testInsertPOD);
137 FAIL_UNLESS(testInsertString);
138 return kPassed;
139 }
140