-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.cc
73 lines (64 loc) · 1.92 KB
/
test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// lib function
// should work standalone, only core C++
// no UB, no exceptions
// context: online shop, frontend in browser, backend server
// text box with quantity of products
// content of textbox is send as a string
// function is called on the string
// parse a string and return int if possible
// parsing from left to right, stop on invalid character
// "123" -> 123 (int)
// "asd" -> 0
// "" -> 0
// "12b4" -> 12
// "a12" -> 0
// "-4" -> 0
// "+4" -> 0
// "3245783492057348905734290857349857340289" -> 0 or (preferred) parsing until possible
// "123.123" -> 123
// const char* not std::string
// INT_MAX
#include <iostream>
int parseQuantity(const char* str);
static int failed = 0;
static int total = 0;
void test(const char* input, int expected) {
auto result = parseQuantity(input);
if (result != expected) {
failed++;
std::cerr << "FAILED: \"" << input << "\" -> expected: " << expected << ", got " << result << '\n';
}
total++;
}
int main() {
test("123", 123);
test("asd", 0);
test("", 0);
test("12b4", 12);
test("a12", 0);
test("-4", 0);
test("+4", 0);
test("123.123", 123);
test("123,123", 123);
test("0", 0);
test("2147483647", 2'147'483'647);
test("002147483647", 2'147'483'647);
test("0021474836471", 2'147'483'647);
test("2147483647123", 2'147'483'647);
test("2147483647000", 2'147'483'647);
test("2147483648", 214'748'364);
test("2147483640234525", 2'147'483'640);
test("9876543210", 987'654'321);
test("098765432109", 987'654'321);
test("00000000000000000000000", 0);
test("000000000000000000000001", 1);
test("00000000000000000000000000000000000000098765432109", 987'654'321);
test(" 123", 0);
std::cout << "------------\n";
std::cout << failed << "/" << total << " failed\n";
std::cout << total - failed << "/" << total << " passed\n";
if (failed == 0) {
return 0;
}
return -1;
}