Skip to content

Commit da65efa

Browse files
committed
Make map.cpp not print anything, assert instead
1 parent bf6a695 commit da65efa

2 files changed

Lines changed: 29 additions & 29 deletions

File tree

‎c/string_h.c‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,8 @@ int main(void) {
158158
assert(strcmp("abc", "aac") > 0);
159159

160160
/*
161-
Different lengths. TODO defined or not? C11:
162-
163-
> The sign of a nonzero value returned by the comparison functions memcmp, strcmp,
164-
and strncmp is determined by the sign of the difference between the values of the first
165-
pair of characters (both interpreted as unsigned char) that differ in the objects being
166-
compared.
167-
168-
So it looks like '\0' == 0 and is smaller than everything, and it is part of the "string object".
169-
170-
https://stackoverflow.com/questions/36518931/what-does-strcmp-return-if-two-similar-strings-are-of-different-lengths
171-
*/
161+
Different lengths. '\0' is smaller than all.
162+
https://stackoverflow.com/questions/36518931/what-does-strcmp-return-if-two-similar-strings-are-of-different-lengths/47366149#47366149*/
172163
assert(strcmp("a", "abc") < 0);
173164
}
174165

‎cpp/map.cpp‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -248,31 +248,40 @@ int main() {
248248

249249
// # Range switch case.
250250
// http://stackoverflow.com/questions/9432226/how-do-i-select-a-range-of-values-in-a-switch-statement/42331563#42331563
251-
#if 0
252251
{
252+
auto result = std::map<int, int>{
253+
{-1, -1},
254+
{ 0, 0},
255+
{ 1, 0},
256+
{ 2, 2},
257+
{ 3, 2},
258+
{ 4, 2},
259+
{ 5, 5},
260+
{ 6, 5},
261+
{ 7, 7},
262+
};
263+
std::map<int,std::function<void()>> m;
264+
int x;
265+
m.emplace(0, [&](){
266+
x = -1;
267+
});
268+
m.emplace(2, [&](){
269+
x = 0;
270+
});
271+
m.emplace(5, [&](){
272+
x = 2;
273+
});
274+
m.emplace(7, [&](){
275+
x = 5;
276+
});
253277
for (auto i = -1; i < 8; ++i) {
254-
std::cout << i << std::endl;
255-
std::map<int,std::function<void()>> m;
256-
m.emplace(0, [](){
257-
std::cout << "too small" << std::endl;
258-
});
259-
m.emplace(2, [](){
260-
std::cout << "zero" << std::endl;
261-
});
262-
m.emplace(5, [](){
263-
std::cout << "two" << std::endl;
264-
});
265-
m.emplace(7, [](){
266-
std::cout << "five" << std::endl;
267-
});
268278
auto it = m.upper_bound(i);
269279
if (it == m.end()) {
270-
std::cout << "too large" << std::endl;
280+
x = 7;
271281
} else {
272282
it->second();
273283
}
274-
std::cout << std::endl;
284+
assert(x == result[i]);
275285
}
276286
}
277-
#endif
278287
}

0 commit comments

Comments
 (0)