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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include "TestSupport.h"
#include "Utils/VariantMap.h"
using namespace Passenger;
namespace tut {
struct VariantMapTest {
VariantMap map;
};
DEFINE_TEST_GROUP(VariantMapTest);
TEST_METHOD(1) {
// Test empty map.
ensure_equals(map.size(), 0u);
ensure(!map.has("hello"));
ensure(!map.has("foo"));
}
TEST_METHOD(2) {
// Test setting and getting string values.
map.set("hello", "world");
map.set("abcd", "efgh");
map.set("foo", "");
map.set("", "bar");
ensure_equals(map.get("hello"), "world");
ensure_equals(map.get("abcd"), "efgh");
ensure_equals(map.get("foo"), "");
ensure_equals(map.get(""), "bar");
ensure_equals(map.size(), 4u);
ensure(map.has("hello"));
ensure(map.has("abcd"));
ensure(map.has("foo"));
ensure(map.has(""));
ensure(!map.has("xyz"));
}
TEST_METHOD(3) {
// Test setting and getting non-string values.
map.set("str", "1234");
map.setInt("int", 5678);
map.setULL("ull", 18446744073709551615ull);
map.setPid("pid", 47326);
map.setUid("uid", (uid_t) 500);
map.setGid("gid", (gid_t) 510);
map.setUid("negative_uid", (uid_t) -1);
map.setGid("negative_gid", (gid_t) -2);
map.setBool("true", true);
map.setBool("false", false);
ensure_equals(map.size(), 10u);
ensure(map.has("str"));
ensure(map.has("int"));
ensure(map.has("ull"));
ensure(map.has("pid"));
ensure(map.has("uid"));
ensure(map.has("gid"));
ensure(map.has("negative_uid"));
ensure(map.has("negative_gid"));
ensure(map.has("true"));
ensure(map.has("false"));
ensure(!map.has("foo"));
ensure_equals(map.get("str"), "1234");
ensure_equals(map.get("int"), "5678");
ensure_equals(map.get("ull"), "18446744073709551615");
ensure_equals(map.get("pid"), "47326");
ensure_equals(map.get("uid"), "500");
ensure_equals(map.get("gid"), "510");
// No idea how negative_uid and negative_gid are casted to string;
// depends on whether the platform defines them as signed or unsigned.
ensure_equals(map.get("true"), "true");
ensure_equals(map.get("false"), "false");
ensure_equals(map.getInt("str"), 1234);
ensure_equals(map.getInt("int"), 5678);
ensure_equals(map.getInt("pid"), 47326);
ensure_equals(map.getInt("uid"), 500);
ensure_equals(map.getInt("gid"), 510);
if (sizeof(uid_t) == sizeof(int)) {
ensure_equals(map.getInt("negative_uid"), -1);
}
if (sizeof(gid_t) == sizeof(int)) {
ensure_equals(map.getInt("negative_gid"), -2);
}
ensure_equals(map.getULL("ull"), 18446744073709551615ull);
ensure_equals(map.getPid("pid"), (pid_t) 47326);
ensure_equals(map.getUid("uid"), (uid_t) 500);
ensure_equals(map.getGid("gid"), (gid_t) 510);
ensure_equals(map.getUid("negative_uid"), (uid_t) -1);
ensure_equals(map.getGid("negative_gid"), (gid_t) -2);
ensure_equals(map.getBool("true"), true);
ensure_equals(map.getBool("false"), false);
}
TEST_METHOD(4) {
// get() throws MissingKeyException if the key doesn't
// exist and 'required' is true (which it is by default).
try {
map.get("str");
fail("MissingKeyException expected (str)");
} catch (const VariantMap::MissingKeyException &e) {
ensure_equals(e.getKey(), "str");
}
try {
map.getInt("int");
fail("MissingKeyException expected (int)");
} catch (const VariantMap::MissingKeyException &e) {
ensure_equals(e.getKey(), "int");
}
try {
map.getULL("ull");
fail("MissingKeyException expected (ull)");
} catch (const VariantMap::MissingKeyException &e) {
ensure_equals(e.getKey(), "ull");
}
try {
map.getPid("pid");
fail("MissingKeyException expected (pid)");
} catch (const VariantMap::MissingKeyException &e) {
ensure_equals(e.getKey(), "pid");
}
try {
map.getUid("uid");
fail("MissingKeyException expected (uid)");
} catch (const VariantMap::MissingKeyException &e) {
ensure_equals(e.getKey(), "uid");
}
try {
map.getGid("gid");
fail("MissingKeyException expected (gid)");
} catch (const VariantMap::MissingKeyException &e) {
ensure_equals(e.getKey(), "gid");
}
try {
map.getBool("bool");
fail("MissingKeyException expected (bool)");
} catch (const VariantMap::MissingKeyException &e) {
ensure_equals(e.getKey(), "bool");
}
}
TEST_METHOD(5) {
// get() returns the default value if 'required' is false.
ensure_equals(map.get("foo", false, "1234"), "1234");
ensure_equals(map.getInt("foo", false, 1234), 1234);
ensure_equals(map.getULL("foo", false, 18446744073709551615ull), 18446744073709551615ull);
ensure_equals(map.getPid("foo", false, 1234), (pid_t) 1234);
ensure_equals(map.getUid("foo", false, 1234), (gid_t) 1234);
ensure_equals(map.getGid("foo", false, 1234), (uid_t) 1234);
ensure_equals(map.getBool("foo", false, true), true);
ensure_equals(map.getBool("foo", false, false), false);
}
TEST_METHOD(6) {
// Test populating from array.
const char *ary[] = {
"foo", "1234",
"bar", "5678"
};
try {
map.readFrom(ary, 3);
fail("ArgumentException expected");
} catch (const ArgumentException &) {
// Pass.
}
map.readFrom(ary, 4);
ensure_equals(map.get("foo"), "1234");
ensure_equals(map.get("bar"), "5678");
}
}
|