Files
tilemaker/test/attribute_store.test.cpp
Colin Dellow 93f618574f AttributePair: use uint, not char
Since we only allocate 4 bits and `char` is signed, the usable space
was -8..7. Larger values (like, say, 12) overflow, and get interpreted
as a negative value, which means they don't act as a filter, since all
zoom values are natural numbers.

The tests didn't actually test that a zoom value could be roundtripped.
I updated them, and verified they failed before the code change, and
passed after the code change.

I've also allocated an extra bit so that we support minzooms up to z31,
vs just up to z15, since I think (?) some people generate up to z16
2024-02-06 20:54:20 -05:00

106 lines
2.9 KiB
C++

#include <iostream>
#include <algorithm>
#include "external/minunit.h"
#include "attribute_store.h"
MU_TEST(test_attribute_store) {
AttributeStore store;
store.reset();
mu_check(store.size() == 0);
AttributeSet s1;
store.addAttribute(s1, "str1", std::string("someval"), 0);
store.addAttribute(s1, "str2", std::string("a very long string"), 14);
store.addAttribute(s1, "bool1", false, 0);
store.addAttribute(s1, "bool2", true, 0);
store.addAttribute(s1, "float1", (float)42.0, 4);
const auto s1Index = store.add(s1);
mu_check(store.size() == 1);
const auto s1Pairs = store.getUnsafe(s1Index);
mu_check(s1Pairs.size() == 5);
const auto str1 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("str1");
});
mu_check(str1 != s1Pairs.end());
mu_check((*str1)->hasStringValue());
mu_check((*str1)->stringValue() == "someval");
const auto str2 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("str2");
});
mu_check(str2 != s1Pairs.end());
mu_check((*str2)->hasStringValue());
mu_check((*str2)->stringValue() == "a very long string");
mu_check((*str2)->minzoom == 14);
const auto bool1 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("bool1");
});
mu_check(bool1 != s1Pairs.end());
mu_check((*bool1)->hasBoolValue());
mu_check((*bool1)->boolValue() == false);
const auto bool2 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("bool2");
});
mu_check(bool2 != s1Pairs.end());
mu_check((*bool2)->hasBoolValue());
mu_check((*bool2)->boolValue() == true);
const auto float1 = std::find_if(s1Pairs.begin(), s1Pairs.end(), [&store](auto ap) {
return ap->keyIndex == store.keyStore.key2index("float1");
});
mu_check(float1 != s1Pairs.end());
mu_check((*float1)->hasFloatValue());
mu_check((*float1)->floatValue() == 42);
mu_check((*float1)->minzoom == 4);
}
MU_TEST(test_attribute_store_reuses) {
AttributeStore store;
store.reset();
mu_check(store.size() == 0);
{
AttributeSet s1a;
store.addAttribute(s1a, "str1", std::string("someval"), 0);
const auto s1aIndex = store.add(s1a);
AttributeSet s1b;
store.addAttribute(s1b, "str1", std::string("someval"), 0);
const auto s1bIndex = store.add(s1b);
mu_check(s1aIndex == s1bIndex);
}
{
AttributeSet s1a;
store.addAttribute(s1a, "str1", std::string("this is a very long string"), 0);
const auto s1aIndex = store.add(s1a);
AttributeSet s1b;
store.addAttribute(s1b, "str1", std::string("this is a very long string"), 0);
const auto s1bIndex = store.add(s1b);
mu_check(s1aIndex == s1bIndex);
}
}
MU_TEST_SUITE(test_suite_attribute_store) {
MU_RUN_TEST(test_attribute_store);
MU_RUN_TEST(test_attribute_store_reuses);
}
int main() {
MU_RUN_SUITE(test_suite_attribute_store);
MU_REPORT();
return MU_EXIT_CODE;
}