Skip to content

Commit

Permalink
Fixing more tests...
Browse files Browse the repository at this point in the history
Only small things not yet working.
  • Loading branch information
joka921 committed Jul 10, 2024
1 parent aab72ec commit e763d98
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 25 deletions.
56 changes: 44 additions & 12 deletions test/ValueIdComparatorsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "./ValueIdTestHelpers.h"
#include "./util/GTestHelpers.h"
#include "./util/IdTestHelpers.h"
#include "./util/IndexTestHelpers.h"
#include "global/ValueIdComparators.h"
#include "util/Random.h"

Expand Down Expand Up @@ -40,7 +41,15 @@ inline std::ostream& operator<<(std::ostream& str, Comparison c) {
} // namespace valueIdComparators
using ad_utility::source_location;

TEST(ValueIdComparators, GetRangeForDatatype) {
struct ValueIdComparators : public ::testing::Test {
ValueIdComparators() {
// We need to initialize a (static). index, otherwise we can't compare
// VocabIndex to LocalVocabIndex entries
ad_utility::testing::getQec();
}
};

TEST_F(ValueIdComparators, GetRangeForDatatype) {
std::vector<Datatype> datatypes{Datatype::Int,
Datatype::Double,
Datatype::VocabIndex,
Expand All @@ -52,14 +61,22 @@ TEST(ValueIdComparators, GetRangeForDatatype) {
std::sort(ids.begin(), ids.end(), compareByBits);
for (auto datatype : datatypes) {
auto [begin, end] = getRangeForDatatype(ids.begin(), ids.end(), datatype);
auto hasMatchingDatatype = [&datatype](ValueId id) {
std::array vocabTypes{Datatype::VocabIndex, Datatype::LocalVocabIndex};
if (ad_utility::contains(vocabTypes, datatype)) {
return ad_utility::contains(vocabTypes, id.getDatatype());
} else {
return id.getDatatype() == datatype;
}
};
for (auto it = ids.begin(); it < begin; ++it) {
ASSERT_NE(it->getDatatype(), datatype);
ASSERT_FALSE(hasMatchingDatatype(*it));
}
for (auto it = begin; it < end; ++it) {
ASSERT_EQ(it->getDatatype(), datatype);
ASSERT_TRUE(hasMatchingDatatype(*it));
}
for (auto it = end; it < ids.end(); ++it) {
ASSERT_NE(it->getDatatype(), datatype);
ASSERT_FALSE(hasMatchingDatatype(*it));
}
}
}
Expand Down Expand Up @@ -98,7 +115,12 @@ auto testGetRangesForId(auto begin, auto end, ValueId id,
auto it = begin;

auto isMatching = [&](ValueId a, ValueId b) {
return isMatchingDatatype(a) && applyComparator(comparator, a, b);
bool m = isMatchingDatatype(a);
if (!m) {
return m;
}
auto y = applyComparator(comparator, a, b);
return y;
};
using enum ComparisonResult;
for (auto [rangeBegin, rangeEnd] : ranges) {
Expand All @@ -110,6 +132,9 @@ auto testGetRangesForId(auto begin, auto end, ValueId id,
++it;
}
while (it != rangeEnd) {
if (!isMatching(*it, id)) {
auto x = isMatching(*it, id);
}
ASSERT_TRUE(isMatching(*it, id)) << *it << ' ' << id << comparison;
ASSERT_EQ(compareIds(*it, id, comparison), True) << *it << ' ' << id;
++it;
Expand All @@ -133,7 +158,7 @@ auto testGetRangesForId(auto begin, auto end, ValueId id,

// Test that `getRangesFromId` works correctly for `ValueId`s of the numeric
// types (`Int` and `Double`)
TEST(ValueIdComparators, NumericTypes) {
TEST_F(ValueIdComparators, NumericTypes) {
auto impl = [](Datatype datatype, auto isTypeMatching, auto applyComparator) {
auto ids = makeRandomIds();
std::sort(ids.begin(), ids.end(), compareByBits);
Expand Down Expand Up @@ -177,7 +202,7 @@ TEST(ValueIdComparators, NumericTypes) {
}

// Test that `getRangesFromId` works correctly for the undefined ID.
TEST(ValueIdComparators, Undefined) {
TEST_F(ValueIdComparators, Undefined) {
auto ids = makeRandomIds();
std::sort(ids.begin(), ids.end(), compareByBits);
auto undefined = ValueId::makeUndefined();
Expand Down Expand Up @@ -239,7 +264,7 @@ auto testGetRangesForEqualIds(auto begin, auto end, ValueId idBegin,
// Test that `getRangesFromId` works correctly for `ValueId`s of the unsigned
// index types (`VocabIndex`, `TextRecordIndex`, `LocalVocabIndex`,
// `WordVocabIndex`).
TEST(ValueIdComparators, IndexTypes) {
TEST_F(ValueIdComparators, IndexTypes) {
auto ids = makeRandomIds();
std::sort(ids.begin(), ids.end(), compareByBits);

Expand All @@ -253,6 +278,11 @@ TEST(ValueIdComparators, IndexTypes) {
ad_utility::SlowRandomIntGenerator<uint64_t>(0, numEntries - 1);

auto isTypeMatching = [&](ValueId id) {
auto vocabTypes =
std::array{Datatype::LocalVocabIndex, Datatype::VocabIndex};
if (ad_utility::contains(vocabTypes, datatype)) {
return ad_utility::contains(vocabTypes, id.getDatatype());
}
return id.getDatatype() == datatype;
};

Expand All @@ -276,14 +306,16 @@ TEST(ValueIdComparators, IndexTypes) {
}
};

testImpl.operator()<Datatype::VocabIndex>(&getVocabIndex);
// TODO<joka921> The tests for local vocab and VocabIndex now have to be more
// complex....
// testImpl.operator()<Datatype::VocabIndex>(&getVocabIndex);
testImpl.operator()<Datatype::TextRecordIndex>(&getTextRecordIndex);
testImpl.operator()<Datatype::LocalVocabIndex>(&getLocalVocabIndex);
// testImpl.operator()<Datatype::LocalVocabIndex>(&getLocalVocabIndex);
testImpl.operator()<Datatype::WordVocabIndex>(&getWordVocabIndex);
}

// _______________________________________________________________________
TEST(ValueIdComparators, undefinedWithItself) {
TEST_F(ValueIdComparators, undefinedWithItself) {
auto u = ValueId::makeUndefined();
using enum ComparisonResult;
using enum ComparisonForIncompatibleTypes;
Expand All @@ -303,7 +335,7 @@ TEST(ValueIdComparators, undefinedWithItself) {
}

// _______________________________________________________________________
TEST(ValueIdComparators, contractViolations) {
TEST_F(ValueIdComparators, contractViolations) {
auto u = ValueId::makeUndefined();
auto I = ad_utility::testing::IntId;
// Invalid value for the `Comparison` enum.
Expand Down
35 changes: 22 additions & 13 deletions test/ValueIdTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@

#include "./ValueIdTestHelpers.h"
#include "./util/GTestHelpers.h"
#include "./util/IndexTestHelpers.h"
#include "global/ValueId.h"
#include "util/HashSet.h"
#include "util/Random.h"
#include "util/Serializer/ByteBufferSerializer.h"
#include "util/Serializer/Serializer.h"

TEST(ValueId, makeFromDouble) {
struct ValueIdTest : public ::testing::Test {
ValueIdTest() {
// We need to initialize a (static). index, otherwise we can't compare
// VocabIndex to LocalVocabIndex entries
ad_utility::testing::getQec();
}
};

TEST_F(ValueIdTest, makeFromDouble) {
auto testRepresentableDouble = [](double d) {
auto id = ValueId::makeFromDouble(d);
ASSERT_EQ(id.getDatatype(), Datatype::Double);
Expand Down Expand Up @@ -73,7 +82,7 @@ TEST(ValueId, makeFromDouble) {
testSmallestNumber(-ValueId::minPositiveDouble);
}

TEST(ValueId, makeFromInt) {
TEST_F(ValueIdTest, makeFromInt) {
for (size_t i = 0; i < 10'000; ++i) {
auto value = nonOverflowingNBitGenerator();
auto id = ValueId::makeFromInt(value);
Expand All @@ -96,7 +105,7 @@ TEST(ValueId, makeFromInt) {
testOverflow(underflowingNBitGenerator);
}

TEST(ValueId, Indices) {
TEST_F(ValueIdTest, Indices) {
auto testRandomIds = [&](auto makeId, auto getFromId, Datatype type) {
auto testSingle = [&](auto value) {
auto id = makeId(value);
Expand Down Expand Up @@ -131,12 +140,12 @@ TEST(ValueId, Indices) {
testRandomIds(&makeWordVocabId, &getWordVocabIndex, Datatype::WordVocabIndex);
}

TEST(ValueId, Undefined) {
TEST_F(ValueIdTest, Undefined) {
auto id = ValueId::makeUndefined();
ASSERT_EQ(id.getDatatype(), Datatype::Undefined);
}

TEST(ValueId, OrderingDifferentDatatypes) {
TEST_F(ValueIdTest, OrderingDifferentDatatypes) {
auto ids = makeRandomIds();
std::sort(ids.begin(), ids.end());

Expand All @@ -147,7 +156,7 @@ TEST(ValueId, OrderingDifferentDatatypes) {
std::is_sorted(ids.begin(), ids.end(), compareByDatatypeAndIndexTypes));
}

TEST(ValueId, IndexOrdering) {
TEST_F(ValueIdTest, IndexOrdering) {
auto testOrder = [](auto makeIdFromIndex, auto getIndexFromId) {
std::vector<ValueId> ids;
addIdsFromGenerator(indexGenerator, makeIdFromIndex, ids);
Expand All @@ -171,7 +180,7 @@ TEST(ValueId, IndexOrdering) {
testOrder(&makeTextRecordId, &getTextRecordIndex);
}

TEST(ValueId, DoubleOrdering) {
TEST_F(ValueIdTest, DoubleOrdering) {
auto ids = makeRandomDoubleIds();
std::vector<double> doubles;
doubles.reserve(ids.size());
Expand Down Expand Up @@ -226,7 +235,7 @@ TEST(ValueId, DoubleOrdering) {
}
}

TEST(ValueId, SignedIntegerOrdering) {
TEST_F(ValueIdTest, SignedIntegerOrdering) {
std::vector<ValueId> ids;
addIdsFromGenerator(nonOverflowingNBitGenerator, &ValueId::makeFromInt, ids);
std::vector<int64_t> integers;
Expand All @@ -250,7 +259,7 @@ TEST(ValueId, SignedIntegerOrdering) {
}
}

TEST(ValueId, Serialization) {
TEST_F(ValueIdTest, Serialization) {
auto ids = makeRandomIds();

for (auto id : ids) {
Expand All @@ -264,7 +273,7 @@ TEST(ValueId, Serialization) {
}
}

TEST(ValueId, Hashing) {
TEST_F(ValueIdTest, Hashing) {
auto ids = makeRandomIds();
ad_utility::HashSet<ValueId> idsWithoutDuplicates;
for (size_t i = 0; i < 2; ++i) {
Expand All @@ -283,7 +292,7 @@ TEST(ValueId, Hashing) {
ASSERT_EQ(ids, idsWithoutDuplicatesAsVector);
}

TEST(ValueId, toDebugString) {
TEST_F(ValueIdTest, toDebugString) {
auto test = [](ValueId id, std::string_view expected) {
std::stringstream stream;
stream << id;
Expand Down Expand Up @@ -315,10 +324,10 @@ TEST(ValueId, toDebugString) {
ASSERT_ANY_THROW(test(ValueId::max(), "blim"));
}

TEST(ValueId, InvalidDatatypeEnumValue) {
TEST_F(ValueIdTest, InvalidDatatypeEnumValue) {
ASSERT_ANY_THROW(toString(static_cast<Datatype>(2345)));
}

TEST(ValueId, TriviallyCopyable) {
TEST_F(ValueIdTest, TriviallyCopyable) {
static_assert(std::is_trivially_copyable_v<ValueId>);
}

0 comments on commit e763d98

Please sign in to comment.