Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unordered_inline_set #546

Merged
merged 1 commit into from
Aug 26, 2024
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
85 changes: 85 additions & 0 deletions common/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
#define protected public
#define private public

#include "../unordered_inline_set.h"
#include "../generator.h"
#include "../estring.h"
#include "../alog.cpp"
Expand Down Expand Up @@ -1257,6 +1258,90 @@ TEST(update_now, after_idle_sleep) {
EXPECT_GT(after_, before);
}

TEST(SparseArray, test0) {
SparseArray<int> a(100);
EXPECT_TRUE(a.empty());
a.set(1, 123);
EXPECT_FALSE(a.empty());
a.set(45, 456);
a.set(79, 79);
a.set(79, 789);
a.emplace(23, 234);
a.emplace(65, 654);
EXPECT_EQ(a.size(), 5);
EXPECT_EQ(a[1], 123);
EXPECT_EQ(a[45], 456);
EXPECT_EQ(a[79], 789);
EXPECT_EQ(a[23], 234);
EXPECT_EQ(a[65], 654);
vector<int> va(a.begin(), a.end()),
vb{123, 234, 456, 654, 789};
EXPECT_EQ(va, vb);

auto it = a.begin();
EXPECT_EQ(*++it, 234);
EXPECT_EQ(*++it, 456);
EXPECT_EQ(*++it, 654);
EXPECT_EQ(*--it, 456);
EXPECT_EQ(*--it, 234);
EXPECT_EQ(*--it, 123);
}

static int num_of_a = 0;

TEST(SparseArray, test1) {
class A {
public:
float a, b = ++num_of_a;
A() = default;
A(const A& rhs) { a = rhs.a; };
A(double x) { a = x; }
bool operator==(const A& rhs) const { return a == rhs.a; }
~A() { --num_of_a; }
};
{
SparseArray<A> a(100);
EXPECT_TRUE(a.empty());
a.set(1, 123);
EXPECT_FALSE(a.empty());
a.set(45, 456);
a.set(79, 79);
a.set(79, 789);
a.emplace(23, 34);
a.emplace(23, 234);
a.emplace(65, 654);
EXPECT_EQ(a.size(), 5);
EXPECT_EQ(a[1], 123);
EXPECT_EQ(a[45], 456);
EXPECT_EQ(a[79], 789);
EXPECT_EQ(a[23], 234);
EXPECT_EQ(a[65], 654);
EXPECT_EQ(num_of_a, 5);
vector<A> va(a.begin(), a.end());
EXPECT_EQ(num_of_a, 10);
vector<A> vb{123, 234, 456, 654, 789};
EXPECT_EQ(num_of_a, 15);
EXPECT_EQ(va, vb);
}
EXPECT_EQ(num_of_a, 0);
}

TEST(unordered_inline_set, test0) {
static int a[] = {421, 3, 79, 9785234, 7494};
unordered_inline_set<int> set(a, a + LEN(a));
for (auto x: a) {
EXPECT_EQ(set.count(x), 1);
EXPECT_EQ(set.count(-x), 0);
}
vector<int> va(a, a + LEN(a)),
vb(set.begin(), set.end());
sort(va.begin(), va.end());
sort(vb.begin(), vb.end());
EXPECT_EQ(va, vb);
}


#include <vector>
// #endif
int main(int argc, char **argv)
{
Expand Down
Loading
Loading