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 @@ -81,7 +81,6 @@ src/tscore/ParseRulesCTypeToUpper
src/tscore/mkdfa
src/tscore/test_atomic
src/tscore/test_freelist
src/tscore/test_Vec
src/tscore/test_geometry
src/tscore/test_Regex
src/tscore/test_X509HostnameValidator
Expand Down
8 changes: 3 additions & 5 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_atomic test_freelist test_geometry test_Vec test_X509HostnameValidator test_tscore
check_PROGRAMS = test_atomic test_freelist test_geometry test_X509HostnameValidator test_tscore

TESTS_ENVIRONMENT = LSAN_OPTIONS=suppressions=suppression.txt

Expand Down Expand Up @@ -237,9 +237,6 @@ test_atomic_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la
test_freelist_SOURCES = test_freelist.cc
test_freelist_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@

test_geometry_SOURCES = test_geometry.cc
test_geometry_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la @LIBTCL@ @LIBPCRE@

Expand Down Expand Up @@ -277,7 +274,8 @@ test_tscore_SOURCES = \
unit_tests/test_Regex.cc \
unit_tests/test_Scalar.cc \
unit_tests/test_scoped_resource.cc \
unit_tests/test_ts_file.cc
unit_tests/test_ts_file.cc \
unit_tests/test_Vec.cc

CompileParseRules_SOURCES = CompileParseRules.cc

Expand Down
82 changes: 36 additions & 46 deletions src/tscore/test_Vec.cc → src/tscore/unit_tests/test_Vec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

/* UnionFind after Tarjan */

#include "catch.hpp"

#include <cstdint>
#include <cstdio>
#include "tscore/ink_assert.h"
Expand Down Expand Up @@ -202,34 +204,32 @@ UnionFind::unify(int n, int m)
}
}

static void
test_append()
TEST_CASE("test append", "[Vec]")
{
static const char value[] = "this is a string";
unsigned int len = (int)sizeof(value) - 1;

Vec<char> str;

str.append(value, 0);
ink_assert(str.length() == 0);
REQUIRE(str.length() == 0);

str.append(value, len);
ink_assert(memcmp(&str[0], value, len) == 0);
ink_assert(str.length() == len);
REQUIRE(memcmp(&str[0], value, len) == 0);
REQUIRE(str.length() == len);

str.clear();
ink_assert(str.length() == 0);
REQUIRE(str.length() == 0);

for (unsigned i = 0; i < 1000; ++i) {
str.append(value, len);
ink_assert(memcmp(&str[i * len], value, len) == 0);
REQUIRE(memcmp(&str[i * len], value, len) == 0);
}

ink_assert(str.length() == 1000 * len);
REQUIRE(str.length() == 1000 * len);
}

static void
test_basic()
TEST_CASE("test basic", "[libts][Vec]")
{
Vec<void *> v, vv, vvv;
int tt = 99 * 50, t = 0;
Expand All @@ -240,7 +240,7 @@ test_basic()
for (size_t i = 0; i < 100; i++) {
t += (int)(intptr_t)v.v[i];
}
ink_assert(t == tt);
REQUIRE(t == tt);

t = 0;
for (size_t i = 1; i < 100; i++) {
Expand All @@ -258,7 +258,7 @@ test_basic()
t += (int)(intptr_t)vv.v[i];
}
}
ink_assert(t == tt + 1000 * tt);
REQUIRE(t == tt + 1000 * tt);

v.clear();
v.reserve(1000);
Expand All @@ -269,42 +269,42 @@ test_basic()
for (size_t i = 0; i < 1000; i++) {
t += (int)(intptr_t)v.v[i];
}
ink_assert(t == 999 * 500);
REQUIRE(t == 999 * 500);
printf("%zu %zu\n", v.n, v.i);

Intervals in;
in.insert(1);
ink_assert(in.n == 2);
REQUIRE(in.n == 2);
in.insert(2);
ink_assert(in.n == 2);
REQUIRE(in.n == 2);
in.insert(6);
ink_assert(in.n == 4);
REQUIRE(in.n == 4);
in.insert(7);
ink_assert(in.n == 4);
REQUIRE(in.n == 4);
in.insert(9);
ink_assert(in.n == 6);
REQUIRE(in.n == 6);
in.insert(4);
ink_assert(in.n == 8);
REQUIRE(in.n == 8);
in.insert(5);
ink_assert(in.n == 6);
REQUIRE(in.n == 6);
in.insert(3);
ink_assert(in.n == 4);
REQUIRE(in.n == 4);
in.insert(8);
ink_assert(in.n == 2);
REQUIRE(in.n == 2);

UnionFind uf;
uf.size(4);
uf.unify(0, 1);
uf.unify(2, 3);
ink_assert(uf.find(2) == uf.find(3));
ink_assert(uf.find(0) == uf.find(1));
ink_assert(uf.find(0) != uf.find(3));
ink_assert(uf.find(1) != uf.find(3));
ink_assert(uf.find(1) != uf.find(2));
ink_assert(uf.find(0) != uf.find(2));
REQUIRE(uf.find(2) == uf.find(3));
REQUIRE(uf.find(0) == uf.find(1));
REQUIRE(uf.find(0) != uf.find(3));
REQUIRE(uf.find(1) != uf.find(3));
REQUIRE(uf.find(1) != uf.find(2));
REQUIRE(uf.find(0) != uf.find(2));
uf.unify(1, 2);
ink_assert(uf.find(0) == uf.find(3));
ink_assert(uf.find(1) == uf.find(3));
REQUIRE(uf.find(0) == uf.find(3));
REQUIRE(uf.find(1) == uf.find(3));
}

static bool
Expand All @@ -313,16 +313,15 @@ compare(void *a, void *b)
return a < b;
}

static void
test_sort()
TEST_CASE("test sort", "[libts][Vec]")
{
Vec<void *> v;
for (long i = 1; i <= 1000; ++i) {
v.add(reinterpret_cast<void *>(static_cast<intptr_t>(((i * 149) % 1000) + 1)));
}
v.qsort(&compare);
for (int i = 0; i < 1000; ++i) {
ink_assert(reinterpret_cast<void *>(static_cast<intptr_t>(i + 1)) == v[i]);
REQUIRE(reinterpret_cast<void *>(static_cast<intptr_t>(i + 1)) == v[i]);
}

v.clear();
Expand All @@ -332,7 +331,7 @@ test_sort()
v.qsort(&compare);

for (long i = 0; i < 1000000; ++i) {
ink_assert(reinterpret_cast<void *>(static_cast<intptr_t>(i + 1)) == v[i]);
REQUIRE(reinterpret_cast<void *>(static_cast<intptr_t>(i + 1)) == v[i]);
}

v.clear();
Expand All @@ -343,7 +342,7 @@ test_sort()
v.qsort(&compare);

for (long i = 0; i < 1000000; ++i) {
ink_assert(reinterpret_cast<void *>(static_cast<intptr_t>((i / 2) + 1)) == v[i]);
REQUIRE(reinterpret_cast<void *>(static_cast<intptr_t>((i / 2) + 1)) == v[i]);
}

// Very long array, already sorted. This is what broke before.
Expand All @@ -353,7 +352,7 @@ test_sort()
}
v.qsort(&compare);
for (long i = 0; i < 10000000; ++i) {
ink_assert(reinterpret_cast<void *>(static_cast<intptr_t>(i + 1)) == v[i]);
REQUIRE(reinterpret_cast<void *>(static_cast<intptr_t>(i + 1)) == v[i]);
}

// very long, reverse sorted.
Expand All @@ -363,15 +362,6 @@ test_sort()
}
v.qsort(&compare);
for (long i = 0; i < 10000000; ++i) {
ink_assert(reinterpret_cast<void *>(static_cast<intptr_t>(i + 1)) == v[i]);
REQUIRE(reinterpret_cast<void *>(static_cast<intptr_t>(i + 1)) == v[i]);
}
}

int
main(int /* argc ATS_UNUSED */, char ** /* argv ATS_UNUSED */)
{
test_append();
test_basic();
test_sort();
printf("test_Vec PASSED\n");
}