From 755785799bed70b0320f739245af5443530d5c78 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 27 Aug 2013 09:15:49 -0400 Subject: [PATCH] index: Make sure we can index into keys that don't exist This should just return a set of size 0. Signed-off-by: Anna Schumaker --- include/index.h | 2 +- lib/index.cpp | 2 +- tests/index/index.cpp | 11 +++++++++++ tests/index/index.good | 2 ++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/index.h b/include/index.h index 39c657b4..f51cd609 100644 --- a/include/index.h +++ b/include/index.h @@ -27,7 +27,7 @@ public: const std::set::iterator keys_begin(); const std::set::iterator keys_end(); - const std::set &operator[](std::string &); + const std::set &operator[](const std::string &); }; #endif /* OCARINA_INDEX_H */ diff --git a/lib/index.cpp b/lib/index.cpp index 5d844fbc..4b0694b1 100644 --- a/lib/index.cpp +++ b/lib/index.cpp @@ -84,7 +84,7 @@ const std::set::iterator Index :: keys_end() return keys.end(); } -const std::set &Index :: operator[](std::string &key) +const std::set &Index :: operator[](const std::string &key) { return index[key]; } diff --git a/tests/index/index.cpp b/tests/index/index.cpp index 0b3f0f8f..7add515f 100644 --- a/tests/index/index.cpp +++ b/tests/index/index.cpp @@ -138,6 +138,16 @@ void test_4() print_index(index); } +/* + * Test access to keys that don't exist + */ +void test_5() +{ + print("Test 5\n"); + Index index(""); + print("index[nokey].size() = %u\n", index["nokey"].size()); +} + int main(int argc, char **argv) { test_0(); @@ -145,5 +155,6 @@ int main(int argc, char **argv) test_2(); test_3(); test_4(); + test_5(); return 0; } diff --git a/tests/index/index.good b/tests/index/index.good index db47f157..d6c4e5fa 100644 --- a/tests/index/index.good +++ b/tests/index/index.good @@ -102,3 +102,5 @@ index[x] = {1 2 3 4 5 6 7 8 9} index[y] = {2 3 4 5 6 7 8 9} index[z] = {3 4 5 6 7 8 9} +Test 5 +index[nokey].size() = 0