ready to finish erase

This commit is contained in:
2024-03-26 08:20:30 +00:00
parent 68d185f12a
commit e27b18df67
3 changed files with 207 additions and 6 deletions

View File

@ -1,4 +1,6 @@
#include <gtest/gtest.h>
#include <iostream>
#include <random>
#include "map.hpp"
TEST(BasicTests, GTestItSelf) {
@ -12,4 +14,51 @@ TEST(BasicTests, ConstructorAndEmptySize) {
sjtu::map<int, int> map;
EXPECT_EQ(map.empty(), true);
EXPECT_EQ(map.size(), 0);
}
TEST(BasicTests, BasicOperatorInsert) {
sjtu::map<int, int> map1;
map1[1] = 2;
EXPECT_EQ(map1.empty(), false);
EXPECT_EQ(map1.size(), 1);
EXPECT_EQ(map1[1], 2);
auto it = map1.find(1);
EXPECT_EQ(it->second, 2);
sjtu::map<int, int> map2;
auto ret = map2.insert(sjtu::pair<int, int>(1, 2));
EXPECT_EQ(ret.second, true);
EXPECT_EQ(ret.first->first, 1);
EXPECT_EQ(ret.first->second, 2);
}
TEST(BasicTests, MassiveOperatorInsert) {
std::mt19937 rnd(2333);
sjtu::map<int, int> map;
std::map<int, int> standard_map;
int nodes = 1000;
for (int i = 0; i < nodes; ++i) {
int key = rnd() % 10000;
int value = rnd() % 10000;
// std::cerr << "writing " << key << " " << value << "\n";
map[key] = value;
#ifndef NDEBUG
EXPECT_TRUE(map.RedBlackTreeStructureCheck());
#endif
standard_map[key] = value;
}
auto mp_it = map.begin();
auto st_it = standard_map.begin();
EXPECT_EQ(map.size(), standard_map.size());
for (; mp_it != map.end(); mp_it++, st_it++) {
EXPECT_EQ(mp_it->first, st_it->first);
EXPECT_EQ(mp_it->second, st_it->second);
}
mp_it = map.end();
st_it = standard_map.end();
for (int i = 0; i < map.size(); ++i) {
mp_it--;
st_it--;
EXPECT_EQ(mp_it->first, st_it->first);
EXPECT_EQ(mp_it->second, st_it->second);
}
}