Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexStocks committed Jan 26, 2025
1 parent 6d51cf1 commit c6c09eb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 46 deletions.
28 changes: 14 additions & 14 deletions src/kiwi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,29 +363,29 @@ class ZSetsTest {
// ASSERT_TRUE(s.ok());
}

~ZSetsTest() { db.Close(); }
~ZSetsTest() { db.Close(); }

public:
public:
std::string db_path{"./test_db/zsets_test"};
storage::StorageOptions options;
storage::Storage db;
storage::Status s;
};

static bool members_match(const std::vector<std::string> &mm_out, const std::vector<std::string> &expect_members) {
static bool members_match(const std::vector<std::string>& mm_out, const std::vector<std::string>& expect_members) {
if (mm_out.size() != expect_members.size()) {
return false;
}
for (const auto &member : expect_members) {
for (const auto& member : expect_members) {
if (find(mm_out.begin(), mm_out.end(), member) == mm_out.end()) {
return false;
}
}
return true;
}

static bool score_members_match(storage::Storage *const db, const Slice &key,
const std::vector<storage::ScoreMember> &expect_sm) {
static bool score_members_match(storage::Storage* const db, const Slice& key,
const std::vector<storage::ScoreMember>& expect_sm) {
std::vector<storage::ScoreMember> sm_out;
storage::Status s = db->ZRange(key, 0, -1, &sm_out);
if (!s.ok() && !s.IsNotFound()) {
Expand All @@ -405,8 +405,8 @@ static bool score_members_match(storage::Storage *const db, const Slice &key,
return true;
}

static bool score_members_match(const std::vector<storage::ScoreMember> &sm_out,
const std::vector<storage::ScoreMember> &expect_sm) {
static bool score_members_match(const std::vector<storage::ScoreMember>& sm_out,
const std::vector<storage::ScoreMember>& expect_sm) {
if (sm_out.size() != expect_sm.size()) {
return false;
}
Expand All @@ -418,7 +418,7 @@ static bool score_members_match(const std::vector<storage::ScoreMember> &sm_out,
return true;
}

static bool size_match(storage::Storage *const db, const Slice &key, int32_t expect_size) {
static bool size_match(storage::Storage* const db, const Slice& key, int32_t expect_size) {
int32_t size = 0;
storage::Status s = db->ZCard(key, &size);
if (!s.ok() && !s.IsNotFound()) {
Expand All @@ -431,7 +431,7 @@ static bool size_match(storage::Storage *const db, const Slice &key, int32_t exp
return size == expect_size;
}

static bool make_expired(storage::Storage *const db, const storage::Slice &key) {
static bool make_expired(storage::Storage* const db, const storage::Slice& key) {
std::map<storage::DataType, rocksdb::Status> type_status;
int ret = db->Expire(key, 1);
if ((ret == 0) || !type_status[storage::DataType::kZSets].ok()) {
Expand All @@ -441,15 +441,15 @@ static bool make_expired(storage::Storage *const db, const storage::Slice &key)
return true;
}

static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
static bool delete_key(storage::Storage* const db, const storage::Slice& key) {
std::vector<std::string> del_keys = {key.ToString()};
std::map<storage::DataType, storage::Status> type_status;
db->Del(del_keys);
return type_status[storage::DataType::kZSets].ok();
}

void test() {
// NOLINT
// NOLINT
ZSetsTest zt;
int32_t ret = 0;

Expand All @@ -471,8 +471,8 @@ void test() {
printf("sm3 ret = %d\n", ret);
std::map<std::string, double> value_to_dest;
zt.s = zt.db.ZUnionstore("GP1_ZUNIONSTORE_DESTINATION",
{"GP1_ZUNIONSTORE_SM1", "GP1_ZUNIONSTORE_SM2", "GP1_ZUNIONSTORE_SM3"}, {1, 1, 1}, storage::SUM,
value_to_dest, &ret);
{"GP1_ZUNIONSTORE_SM1", "GP1_ZUNIONSTORE_SM2", "GP1_ZUNIONSTORE_SM3"}, {1, 1, 1},
storage::SUM, value_to_dest, &ret);
printf("after union store, sm3 ret = %d\n", ret);
// ASSERT_TRUE(s.ok());
// ASSERT_EQ(ret, 3);
Expand Down
75 changes: 43 additions & 32 deletions src/storage/tests/zsets_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// ASSERT_TRUE(score_members_match(
// score_members, {{2e5 + 3.98, "MM9"}, {2e5 + 3.98, "MM10"}, {1, "MM2"}, {1, "MM1"}, {0.532445, "MM8"}}));
// ASSERT_TRUE(score_members_match(&db, "GP3_ZPOPMAX_KEY",
// {{-1, "MM3"}, {-1, "MM4"}, {1 / 6.0, "MM5"}, {1 / 6.0, "MM6"}, {0.532445, "MM7"}}));
// {{-1, "MM3"}, {-1, "MM4"}, {1 / 6.0, "MM5"}, {1 / 6.0, "MM6"}, {0.532445,
// "MM7"}}));

// // ***************** Group 4 Test *****************
// //
Expand Down Expand Up @@ -398,9 +399,11 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// ASSERT_EQ(5, score_members.size());
// ASSERT_TRUE(size_match(&db, "GP3_ZPOPMIN_KEY", 5));
// ASSERT_TRUE(score_members_match(
// &db, "GP3_ZPOPMIN_KEY", {{0.532445, "MM8"}, {1, "MM1"}, {1, "MM2"}, {2e5 + 3.98, "MM10"}, {2e5 + 3.98, "MM9"}}));
// &db, "GP3_ZPOPMIN_KEY", {{0.532445, "MM8"}, {1, "MM1"}, {1, "MM2"}, {2e5 + 3.98, "MM10"}, {2e5 + 3.98,
// "MM9"}}));
// ASSERT_TRUE(score_members_match(score_members,
// {{-1, "MM3"}, {-1, "MM4"}, {1 / 6.0, "MM5"}, {1 / 6.0, "MM6"}, {0.532445, "MM7"}}));
// {{-1, "MM3"}, {-1, "MM4"}, {1 / 6.0, "MM5"}, {1 / 6.0, "MM6"}, {0.532445,
// "MM7"}}));

// // ***************** Group 4 Test *****************
// //
Expand Down Expand Up @@ -443,7 +446,8 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// ASSERT_TRUE(s.ok());
// ASSERT_EQ(4, ret);
// ASSERT_TRUE(size_match(&db, "GP1_ZADD_KEY", 4));
// ASSERT_TRUE(score_members_match(&db, "GP1_ZADD_KEY", {{-0.54, "MM4"}, {0, "MM2"}, {3.23, "MM1"}, {8.0004, "MM3"}}));
// ASSERT_TRUE(score_members_match(&db, "GP1_ZADD_KEY", {{-0.54, "MM4"}, {0, "MM2"}, {3.23, "MM1"}, {8.0004,
// "MM3"}}));

// // ***************** Group 2 Test *****************
// std::vector<storage::ScoreMember> gp2_sm{{0, "MM1"}, {0, "MM1"}, {0, "MM2"}, {0, "MM3"}};
Expand All @@ -463,10 +467,8 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// {{1 / 7.0, "MM4"}, {1 / 6.0, "MM3"}, {1 / 3.0, "MM2"}, {1 / 1.0, "MM1"}}));

// // ***************** Group 4 Test *****************
// std::vector<storage::ScoreMember> gp4_sm{{-1 / 1.0, "MM1"}, {-1 / 3.0, "MM2"}, {-1 / 6.0, "MM3"}, {-1 / 7.0, "MM4"}};
// s = db.ZAdd("GP4_ZADD_KEY", gp4_sm, &ret);
// ASSERT_TRUE(s.ok());
// ASSERT_EQ(4, ret);
// std::vector<storage::ScoreMember> gp4_sm{{-1 / 1.0, "MM1"}, {-1 / 3.0, "MM2"}, {-1 / 6.0, "MM3"}, {-1 / 7.0,
// "MM4"}}; s = db.ZAdd("GP4_ZADD_KEY", gp4_sm, &ret); ASSERT_TRUE(s.ok()); ASSERT_EQ(4, ret);
// ASSERT_TRUE(size_match(&db, "GP4_ZADD_KEY", 4));
// ASSERT_TRUE(score_members_match(&db, "GP4_ZADD_KEY",
// {{-1 / 1.0, "MM1"}, {-1 / 3.0, "MM2"}, {-1 / 6.0, "MM3"}, {-1 / 7.0, "MM4"}}));
Expand Down Expand Up @@ -505,7 +507,8 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// ASSERT_EQ(1, ret);
// ASSERT_TRUE(size_match(&db, "GP5_ZADD_KEY", 4));
// ASSERT_TRUE(
// score_members_match(&db, "GP5_ZADD_KEY", {{-0.5333, "MM2"}, {0, "MM1"}, {50000, "MM4"}, {1.79769e+308, "MM3"}}));
// score_members_match(&db, "GP5_ZADD_KEY", {{-0.5333, "MM2"}, {0, "MM1"}, {50000, "MM4"}, {1.79769e+308,
// "MM3"}}));

// // [-1.79769e+308, MM5]
// // [-0.5333, MM2]
Expand Down Expand Up @@ -546,7 +549,8 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// ASSERT_TRUE(size_match(&db, "GP5_ZADD_KEY", 6));
// ASSERT_TRUE(score_members_match(
// &db, "GP5_ZADD_KEY",
// {{-1.79769e+308, "MM5"}, {-0.5333, "MM2"}, {0, "MM1"}, {50000, "MM4"}, {100000, "MM6"}, {1.79769e+308, "MM3"}}));
// {{-1.79769e+308, "MM5"}, {-0.5333, "MM2"}, {0, "MM1"}, {50000, "MM4"}, {100000, "MM6"}, {1.79769e+308,
// "MM3"}}));

// // [-1.79769e+308, MM5]
// // [-0.5333, MM2]
Expand Down Expand Up @@ -733,20 +737,17 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// ASSERT_TRUE(s.ok());
// ASSERT_EQ(5, ret);
// ASSERT_TRUE(size_match(&db, "GP2_ZCARD_KEY", 5));
// ASSERT_TRUE(score_members_match(&db, "GP2_ZCARD_KEY", {{1, "MM1"}, {2, "MM2"}, {3, "MM3"}, {4, "MM4"}, {5, "MM5"}}));
// s = db.ZCard("GP2_ZCARD_KEY", &ret);
// ASSERT_TRUE(s.ok());
// ASSERT_EQ(5, ret);
// ASSERT_TRUE(score_members_match(&db, "GP2_ZCARD_KEY", {{1, "MM1"}, {2, "MM2"}, {3, "MM3"}, {4, "MM4"}, {5,
// "MM5"}})); s = db.ZCard("GP2_ZCARD_KEY", &ret); ASSERT_TRUE(s.ok()); ASSERT_EQ(5, ret);

// // ***************** Group 3 Test *****************
// std::vector<storage::ScoreMember> gp3_sm{{1, "MM1"}, {2, "MM2"}, {3, "MM3"}, {4, "MM4"}, {5, "MM5"}};
// s = db.ZAdd("GP3_ZCARD_KEY", gp3_sm, &ret);
// ASSERT_TRUE(s.ok());
// ASSERT_EQ(5, ret);
// ASSERT_TRUE(size_match(&db, "GP3_ZCARD_KEY", 5));
// ASSERT_TRUE(score_members_match(&db, "GP3_ZCARD_KEY", {{1, "MM1"}, {2, "MM2"}, {3, "MM3"}, {4, "MM4"}, {5, "MM5"}}));
// ASSERT_TRUE(make_expired(&db, "GP3_ZCARD_KEY"));
// s = db.ZCard("GP3_ZCARD_KEY", &ret);
// ASSERT_TRUE(score_members_match(&db, "GP3_ZCARD_KEY", {{1, "MM1"}, {2, "MM2"}, {3, "MM3"}, {4, "MM4"}, {5,
// "MM5"}})); ASSERT_TRUE(make_expired(&db, "GP3_ZCARD_KEY")); s = db.ZCard("GP3_ZCARD_KEY", &ret);
// ASSERT_TRUE(s.IsNotFound());
// ASSERT_EQ(0, ret);

Expand All @@ -763,7 +764,8 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {

// // ***************** Group 1 Test *****************
// std::vector<storage::ScoreMember> gp1_sm{{101010.1010101, "MM1"}, {101010.0101010, "MM2"}, {-100.000000001, "MM3"},
// {-100.000000002, "MM4"}, {-100.000000001, "MM5"}, {-100.000000002, "MM6"}};
// {-100.000000002, "MM4"}, {-100.000000001, "MM5"}, {-100.000000002,
// "MM6"}};
// s = db.ZAdd("GP1_ZCOUNT_KEY", gp1_sm, &ret);
// ASSERT_TRUE(s.ok());
// ASSERT_EQ(6, ret);
Expand Down Expand Up @@ -1456,7 +1458,8 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// ASSERT_TRUE(s.ok());
// ASSERT_TRUE(score_members_match(score_members, {}));

// s = db.ZRangebyscore("GP1_ZRANGEBYSCORE_KEY", std::numeric_limits<double>::lowest(), -1000.000000000001, true, true,
// s = db.ZRangebyscore("GP1_ZRANGEBYSCORE_KEY", std::numeric_limits<double>::lowest(), -1000.000000000001, true,
// true,
// &score_members);
// ASSERT_TRUE(s.ok());
// ASSERT_TRUE(score_members_match(score_members, {{-987654321.0000001, "MM1"},
Expand All @@ -1469,7 +1472,8 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// {-1000.000000000001, "MM8"},
// {-1000.000000000001, "MM9"}}));

// s = db.ZRangebyscore("GP1_ZRANGEBYSCORE_KEY", std::numeric_limits<double>::lowest(), -1000.000000000001, true, false,
// s = db.ZRangebyscore("GP1_ZRANGEBYSCORE_KEY", std::numeric_limits<double>::lowest(), -1000.000000000001, true,
// false,
// &score_members);
// ASSERT_TRUE(s.ok());
// ASSERT_TRUE(score_members_match(score_members, {{-987654321.0000001, "MM1"},
Expand Down Expand Up @@ -1574,9 +1578,9 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// ASSERT_TRUE(score_members_match(
// score_members, {{-654321.0000000001, "MM4"}, {-54321.00000000001, "MM5"}, {-4321.000000000001, "MM6"}}));

// s = db.ZRangebyscore("GP1_ZRANGEBYSCORE_KEY", -654321.0000000001, -4321.000000000001, false, false, &score_members);
// ASSERT_TRUE(s.ok());
// ASSERT_TRUE(score_members_match(score_members, {{-54321.00000000001, "MM5"}}));
// s = db.ZRangebyscore("GP1_ZRANGEBYSCORE_KEY", -654321.0000000001, -4321.000000000001, false, false,
// &score_members); ASSERT_TRUE(s.ok()); ASSERT_TRUE(score_members_match(score_members, {{-54321.00000000001,
// "MM5"}}));

// s = db.ZRangebyscore("GP1_ZRANGEBYSCORE_KEY", 0, 0, true, true, &score_members);
// ASSERT_TRUE(s.ok());
Expand Down Expand Up @@ -3661,8 +3665,10 @@ static bool delete_key(storage::Storage *const db, const storage::Slice &key) {
// double score;

// // ***************** Group 1 Test *****************
// std::vector<storage::ScoreMember> gp1_sm{{54354.497895352, "MM1"}, {100.987654321, "MM2"}, {-100.000000001, "MM3"},
// {-100.000000002, "MM4"}, {-100.000000001, "MM5"}, {-100.000000002, "MM6"}};
// std::vector<storage::ScoreMember> gp1_sm{{54354.497895352, "MM1"}, {100.987654321, "MM2"}, {-100.000000001,
// "MM3"},
// {-100.000000002, "MM4"}, {-100.000000001, "MM5"}, {-100.000000002,
// "MM6"}};
// s = db.ZAdd("GP1_ZSCORE_KEY", gp1_sm, &ret);
// ASSERT_TRUE(s.ok());
// ASSERT_EQ(6, ret);
Expand Down Expand Up @@ -4193,8 +4199,8 @@ TEST_F(ZSetsTest, ZUnionstoreTest) {
// s = db.ZAdd("GP10_ZINTERSTORE_SM2", gp10_sm2, &ret);
// s = db.ZAdd("GP10_ZINTERSTORE_SM3", gp10_sm3, &ret);
// s = db.ZInterstore("GP10_ZINTERSTORE_DESTINATION",
// {"GP10_ZINTERSTORE_SM1", "GP10_ZINTERSTORE_SM2", "GP10_ZINTERSTORE_SM3", "GP10_ZINTERSTORE_SM4"},
// {1, 1, 1, 1}, storage::SUM, value_to_dest, &ret);
// {"GP10_ZINTERSTORE_SM1", "GP10_ZINTERSTORE_SM2", "GP10_ZINTERSTORE_SM3",
// "GP10_ZINTERSTORE_SM4"}, {1, 1, 1, 1}, storage::SUM, value_to_dest, &ret);
// ASSERT_TRUE(s.ok());
// ASSERT_EQ(ret, 0);
// ASSERT_TRUE(size_match(&db, "GP10_ZINTERSTORE_DESTINATION", 0));
Expand Down Expand Up @@ -4623,7 +4629,8 @@ TEST_F(ZSetsTest, ZUnionstoreTest) {
// ASSERT_TRUE(size_match(&db, "GP10_ZREMRANGEBYLEX", 9));
// ASSERT_TRUE(
// score_members_match(&db, "GP10_ZREMRANGEBYLEX",
// {{1, "e"}, {1, "f"}, {1, "g"}, {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"}, {1, "l"}, {1, "m"}}));
// {{1, "e"}, {1, "f"}, {1, "g"}, {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"}, {1, "l"}, {1,
// "m"}}));

// // ***************** Group 11 Test *****************
// // {1, e} {1, f} {1, g} {1, h} {1, i} {1, j} {1, k} {1, l} {1, m}
Expand All @@ -4640,7 +4647,8 @@ TEST_F(ZSetsTest, ZUnionstoreTest) {
// ASSERT_TRUE(size_match(&db, "GP11_ZREMRANGEBYLEX", 9));
// ASSERT_TRUE(
// score_members_match(&db, "GP11_ZREMRANGEBYLEX",
// {{1, "e"}, {1, "f"}, {1, "g"}, {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"}, {1, "l"}, {1, "m"}}));
// {{1, "e"}, {1, "f"}, {1, "g"}, {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"}, {1, "l"}, {1,
// "m"}}));

// // ***************** Group 12 Test *****************
// // {1, e} {1, f} {1, g} {1, h} {1, i} {1, j} {1, k} {1, l} {1, m}
Expand All @@ -4657,7 +4665,8 @@ TEST_F(ZSetsTest, ZUnionstoreTest) {
// ASSERT_TRUE(size_match(&db, "GP12_ZREMRANGEBYLEX", 9));
// ASSERT_TRUE(
// score_members_match(&db, "GP12_ZREMRANGEBYLEX",
// {{1, "e"}, {1, "f"}, {1, "g"}, {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"}, {1, "l"}, {1, "m"}}));
// {{1, "e"}, {1, "f"}, {1, "g"}, {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"}, {1, "l"}, {1,
// "m"}}));

// // ***************** Group 13 Test *****************
// // {1, e} {1, f} {1, g} {1, h} {1, i} {1, j} {1, k} {1, l} {1, m}
Expand Down Expand Up @@ -4810,7 +4819,8 @@ TEST_F(ZSetsTest, ZUnionstoreTest) {
// ASSERT_TRUE(size_match(&db, "GP22_ZREMRANGEBYLEX", 9));
// ASSERT_TRUE(
// score_members_match(&db, "GP22_ZREMRANGEBYLEX",
// {{1, "e"}, {1, "f"}, {1, "g"}, {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"}, {1, "l"}, {1, "m"}}));
// {{1, "e"}, {1, "f"}, {1, "g"}, {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"}, {1, "l"}, {1,
// "m"}}));

// // ***************** Group 23 Test *****************
// // {1, e} {1, f} {1, g} {1, h} {1, i} {1, j} {1, k} {1, l} {1, m}
Expand Down Expand Up @@ -4843,7 +4853,8 @@ TEST_F(ZSetsTest, ZUnionstoreTest) {
// ASSERT_TRUE(size_match(&db, "GP24_ZREMRANGEBYLEX", 9));
// ASSERT_TRUE(
// score_members_match(&db, "GP24_ZREMRANGEBYLEX",
// {{1, "e"}, {1, "f"}, {1, "g"}, {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"}, {1, "l"}, {1, "m"}}));
// {{1, "e"}, {1, "f"}, {1, "g"}, {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"}, {1, "l"}, {1,
// "m"}}));

// // ***************** Group 25 Test *****************
// // {1, e} {1, f} {1, g} {1, h} {1, i} {1, j} {1, k} {1, l} {1, m} (expire)
Expand Down

0 comments on commit c6c09eb

Please sign in to comment.