File: StringMapTest.cpp

package info (click to toggle)
ruby-passenger 4.0.53-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,668 kB
  • ctags: 70,512
  • sloc: cpp: 264,280; ruby: 25,606; sh: 22,815; ansic: 18,277; python: 767; makefile: 99; perl: 20
file content (131 lines) | stat: -rw-r--r-- 2,838 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "TestSupport.h"
#include "Utils/StringMap.h"
#include <string>
#include <map>

using namespace Passenger;
using namespace std;

namespace tut {
	struct StringMapTest {
	};
	
	DEFINE_TEST_GROUP(StringMapTest);
	
	TEST_METHOD(1) {
		// get()ing a nonexistant key returns the default value.
		StringMap<string> m;
		ensure_equals(m.get("hello"), "");
	}
	
	TEST_METHOD(2) {
		// set() works.
		StringMap<string> m;
		m.set("hello", "world");
		m.set("foo", "bar");
		ensure_equals(m.get("hello"), "world");
		ensure_equals(m.get("foo"), "bar");
		ensure_equals(m.get("something"), "");
	}
	
	TEST_METHOD(3) {
		// set() overwrites old value.
		StringMap<string> m;
		m.set("hello", "world");
		m.set("foo", "bar");
		m.set("hello", "new world");
		ensure_equals(m.get("hello"), "new world");
		ensure_equals(m.get("foo"), "bar");
	}
	
	TEST_METHOD(4) {
		// The key is interned so changing the original has no effect.
		StringMap<string> m;
		char key1[] = "hello";
		char key2[] = "world";
		
		m.set(key1, "xxx");
		m.set(key2, "yyy");
		
		key1[4] = 'p';
		strcpy(key2, "zzzzz");
		
		ensure_equals(m.get("hello"), "xxx");
		ensure_equals(m.get("hellp"), "");
		
		ensure_equals(m.get("world"), "yyy");
		ensure_equals(m.get("zzzzz"), "");
	}
	
	TEST_METHOD(5) {
		// remove() works.
		StringMap<string> m;
		m.set("hello", "world");
		m.set("foo", "bar");
		
		ensure(m.remove("hello"));
		ensure_equals(m.get("hello"), "");
		ensure_equals(m.get("foo"), "bar");
		ensure(!m.remove("hello"));
	}
	
	TEST_METHOD(6) {
		// Test iterators.
		StringMap<int> m;
		m.set("a", 1);
		m.set("b", 2);
		m.set("c", 3);
		m.set("d", 4);
		
		map<string, int> m2, m3;
		
		StringMap<int>::iterator it, end = m.end();
		for (it = m.begin(); it != end; it++) {
			pair<StaticString, int> p = *it;
			m2[it->first] = it->second;
			m3[p.first] = p.second;
		}
		
		ensure_equals(m2.size(), 4u);
		ensure_equals(m2["a"], 1);
		ensure_equals(m2["b"], 2);
		ensure_equals(m2["c"], 3);
		ensure_equals(m2["d"], 4);
		
		ensure_equals(m3.size(), 4u);
		ensure_equals(m3["a"], 1);
		ensure_equals(m3["b"], 2);
		ensure_equals(m3["c"], 3);
		ensure_equals(m3["d"], 4);
	}
	
	TEST_METHOD(7) {
		// Test const_iterators.
		StringMap<int> m;
		m.set("a", 1);
		m.set("b", 2);
		m.set("c", 3);
		m.set("d", 4);
		
		map<string, int> m2, m3;
		
		StringMap<int>::const_iterator it, end = m.end();
		for (it = m.begin(); it != end; it++) {
			pair<const StaticString, const int> p = *it;
			m2[it->first] = it->second;
			m3[p.first] = p.second;
		}
		
		ensure_equals(m2.size(), 4u);
		ensure_equals(m2["a"], 1);
		ensure_equals(m2["b"], 2);
		ensure_equals(m2["c"], 3);
		ensure_equals(m2["d"], 4);
		
		ensure_equals(m3.size(), 4u);
		ensure_equals(m3["a"], 1);
		ensure_equals(m3["b"], 2);
		ensure_equals(m3["c"], 3);
		ensure_equals(m3["d"], 4);
	}
}