Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ src/tscore/test_List
src/tscore/test_arena
src/tscore/test_atomic
src/tscore/test_freelist
src/tscore/test_Map
src/tscore/test_Vec
src/tscore/test_geometry
src/tscore/test_Regex
Expand Down
6 changes: 2 additions & 4 deletions src/tscore/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
include $(top_srcdir)/build/tidy.mk

noinst_PROGRAMS = mkdfa CompileParseRules
check_PROGRAMS = test_arena test_atomic test_freelist test_geometry test_List test_Map test_Vec test_X509HostnameValidator test_tscore
check_PROGRAMS = test_arena test_atomic test_freelist test_geometry test_List test_Vec test_X509HostnameValidator test_tscore

TESTS_ENVIRONMENT = LSAN_OPTIONS=suppressions=suppression.txt

Expand Down Expand Up @@ -238,9 +238,6 @@ test_arena_SOURCES = test_arena.cc
test_arena_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la @LIBTCL@ @LIBPCRE@

test_List_SOURCES = test_List.cc
test_Map_SOURCES = test_Map.cc
test_Map_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la @LIBTCL@ @LIBPCRE@

test_Vec_SOURCES = test_Vec.cc
test_Vec_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la @LIBTCL@ @LIBPCRE@

Expand Down Expand Up @@ -268,6 +265,7 @@ test_tscore_SOURCES = \
unit_tests/test_IntrusivePtr.cc \
unit_tests/test_IpMap.cc \
unit_tests/test_layout.cc \
unit_tests/test_Map.cc \
unit_tests/test_MemSpan.cc \
unit_tests/test_MemArena.cc \
unit_tests/test_MT_hashtable.cc \
Expand Down
148 changes: 72 additions & 76 deletions src/tscore/test_Map.cc → src/tscore/unit_tests/test_Map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "catch.hpp"

#include <cstdint>
#include "tscore/Map.h"
#include <list>
Expand Down Expand Up @@ -63,67 +66,6 @@ Item::Hash::equal(Key lhs, Key rhs)

using Table = TSHashTable<Item::Hash>;

void
test_TSHashTable()
{
static uint32_t const N = 270;
Table t;
Item *item = nullptr;
Table::Location loc;
std::list<Item *> to_delete;

for (uint32_t i = 1; i <= N; ++i) {
item = new Item(i);
t.insert(item);
to_delete.push_back(item);
}

for (uint32_t i = 1; i <= N; ++i) {
Table::Location l = t.find(i);
ink_assert(l.isValid());
ink_assert(i == l->_value);
}

ink_assert(!(t.find(N * 2).isValid()));

loc = t.find(N / 2 | 1);
if (loc) {
t.remove(loc);
} else {
ink_assert(!"Did not find expected value");
}

if (!loc) {
; // compiler check.
}

ink_assert(!(t.find(N / 2 | 1).isValid()));

for (uint32_t i = 1; i <= N; i += 2) {
t.remove(i);
}

for (uint32_t i = 1; i <= N; ++i) {
Table::Location l = t.find(i);
if (1 & i) {
ink_assert(!l.isValid());
} else {
ink_assert(l.isValid());
}
}

int n = 0;
for (Table::iterator spot = t.begin(), limit = t.end(); spot != limit; ++spot) {
++n;
ink_assert((spot->_value & 1) == 0);
}
ink_assert(n == N / 2);

for (auto it : to_delete) {
delete it;
}
}

class testHashMap
{
private:
Expand All @@ -143,8 +85,7 @@ class testHashMap
}
};

int
main(int /* argc ATS_UNUSED */, char ** /*argv ATS_UNUSED */)
TEST_CASE("test Map", "[libts][Map]")
{
typedef Map<cchar *, cchar *> SSMap;
typedef MapElem<cchar *, cchar *> SSMapElem;
Expand Down Expand Up @@ -177,30 +118,85 @@ main(int /* argc ATS_UNUSED */, char ** /*argv ATS_UNUSED */)
sh.put(ho, 2);
sh.put(hum, 3);
sh.put(hhi, 4);
ink_assert(sh.get(hi) == 4);
ink_assert(sh.get(ho) == 2);
ink_assert(sh.get(hum) == 3);
REQUIRE(sh.get(hi) == 4);
REQUIRE(sh.get(ho) == 2);
REQUIRE(sh.get(hum) == 3);
sh.put("aa", 5);
sh.put("ab", 6);
sh.put("ac", 7);
sh.put("ad", 8);
sh.put("ae", 9);
sh.put("af", 10);
ink_assert(sh.get(hi) == 4);
ink_assert(sh.get(ho) == 2);
ink_assert(sh.get(hum) == 3);
ink_assert(sh.get("af") == 10);
ink_assert(sh.get("ac") == 7);
REQUIRE(sh.get(hi) == 4);
REQUIRE(sh.get(ho) == 2);
REQUIRE(sh.get(hum) == 3);
REQUIRE(sh.get("af") == 10);
REQUIRE(sh.get("ac") == 7);

HashMap<cchar *, StringHashFns, int> sh2(-99); // return -99 if key not found
sh2.put("aa", 15);
sh2.put("ab", 16);
testsh.put("aa", 15);
testsh.put("ab", 16);
ink_assert(sh2.get("aa") == 15);
ink_assert(sh2.get("ac") == -99);
ink_assert(testsh.get("aa") == 15);
test_TSHashTable();
REQUIRE(sh2.get("aa") == 15);
REQUIRE(sh2.get("ac") == -99);
REQUIRE(testsh.get("aa") == 15);

// test_TSHashTable
static uint32_t const N = 270;
Table t;
Item *item = nullptr;
Table::Location loc;
std::list<Item *> to_delete;

for (uint32_t i = 1; i <= N; ++i) {
item = new Item(i);
t.insert(item);
to_delete.push_back(item);
}

for (uint32_t i = 1; i <= N; ++i) {
Table::Location l = t.find(i);
REQUIRE(l.isValid());
REQUIRE(i == l->_value);
}

REQUIRE(!(t.find(N * 2).isValid()));

loc = t.find(N / 2 | 1);
if (loc) {
t.remove(loc);
} else {
REQUIRE(!"Did not find expected value");
}

printf("test_Map PASSED\n");
if (!loc) {
; // compiler check.
}

REQUIRE(!(t.find(N / 2 | 1).isValid()));

for (uint32_t i = 1; i <= N; i += 2) {
t.remove(i);
}

for (uint32_t i = 1; i <= N; ++i) {
Table::Location l = t.find(i);
if (1 & i) {
REQUIRE(!l.isValid());
} else {
REQUIRE(l.isValid());
}
}

int n = 0;
for (Table::iterator spot = t.begin(), limit = t.end(); spot != limit; ++spot) {
++n;
REQUIRE((spot->_value & 1) == 0);
}
REQUIRE(n == N / 2);

for (auto it : to_delete) {
delete it;
}
}