Skip to content

Commit 2ca65ed

Browse files
committed
FIX | critical bug in chain
1 parent 412a6b9 commit 2ca65ed

2 files changed

Lines changed: 97 additions & 7 deletions

File tree

include/std/ManapiChain.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,15 @@ namespace manapi {
225225
if (!prev) {
226226
m = std::move(this->src_);
227227
this->src_ = std::move(next);
228+
this->src_->prev = nullptr;
228229
}
229230
else {
230231
m = std::move(prev->next);
231232
prev->next = std::move(next);
232-
}
233-
234-
if (!next) {
235-
this->last_ = prev;
236-
}
237-
else {
238-
next->prev = prev;
233+
if (prev->next)
234+
prev->next->prev = prev;
235+
else
236+
this->last_ = prev;
239237
}
240238

241239
--this->s_;

tests/test_std.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#include "ManapiString.hpp"
2+
#include "std/ManapiChain.hpp"
3+
#include "ManapiMath.hpp"
4+
5+
#include <deque>
26

37
#include "./utest.h"
48

@@ -43,4 +47,92 @@ UTEST(std_string, str_replace_5) {
4347
ASSERT_TRUE(9 == res);
4448
}
4549

50+
UTEST(std_string, chain_1) {
51+
manapi::chain<int> a;
52+
a.push_back(1);
53+
a.push_back(2);
54+
ASSERT_TRUE(a.size() == 2);
55+
auto a1 = a.back();
56+
a.pop_back();
57+
ASSERT_TRUE(a1 == 2);
58+
auto a2 = a.front();
59+
a.pop_front();
60+
ASSERT_TRUE(a2 == 1);
61+
}
62+
63+
UTEST(std_string, chain_2) {
64+
manapi::chain<int> a;
65+
a.push_back(1);
66+
a.push_back(2);
67+
ASSERT_TRUE(a.size() == 2);
68+
auto a2 = a.front();
69+
a.pop_front();
70+
ASSERT_TRUE(a2 == 1);
71+
auto a1 = a.back();
72+
a.pop_back();
73+
ASSERT_TRUE(a1 == 2);
74+
}
75+
76+
UTEST(std_string, chain_3) {
77+
manapi::chain<int> a;
78+
std::deque<int> b;
79+
80+
for (int i =0 ; i < 500; i++) {
81+
int action = manapi::math::random(0, 8);
82+
int s = manapi::math::random(0, 10000);
83+
ASSERT_TRUE(a.empty() == b.empty());
84+
if (a.empty() || (action % 2 == 0 && action <= 6)) {
85+
b.push_back(s);
86+
a.push_back(s);
87+
}
88+
else if (action % 2 == 1 && action <= 6) {
89+
b.push_front(s);
90+
a.push_front(s);
91+
}
92+
else if (action % 2==0) {
93+
b.pop_back();
94+
a.pop_back();
95+
}
96+
else if (action % 2 == 1) {
97+
b.pop_front();
98+
a.pop_front();
99+
}
100+
}
101+
102+
ASSERT_TRUE(b.size() == a.size());
103+
104+
int cnt=0;
105+
106+
auto bit = b.begin();
107+
auto ait = a.begin();
108+
109+
for (; ait != a.end() && bit != b.end(); ait++, bit++) {
110+
ASSERT_TRUE(*ait == *bit);
111+
cnt++;
112+
}
113+
114+
ASSERT_TRUE(cnt == a.size());
115+
}
116+
117+
UTEST(std_string, chain_4) {
118+
manapi::chain<int> a;
119+
std::vector<int> values;
120+
std::vector<decltype(a)::chain_iterator> its;
121+
122+
for (int i =0 ; i < 500; i++) {
123+
int s = manapi::math::random(0, 100000);
124+
a.push_back(s);
125+
its.push_back(a.rbegin());
126+
values.push_back(s);
127+
}
128+
129+
for (int i = 0; i < 500; i++) {
130+
int p = manapi::math::random(0, a.size() - 1);
131+
ASSERT_TRUE(*its[p] == values[p]);
132+
a.erase(its[p]);
133+
values.erase(values.begin() + p);
134+
its.erase(its.begin() + p);
135+
}
136+
}
137+
46138
UTEST_MAIN();

0 commit comments

Comments
 (0)