-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_test.cpp
73 lines (64 loc) · 1.43 KB
/
csv_test.cpp
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestAssert.h>
int parse_integer(int &value, const char *str, const size_t len)
{
const char *p = str;
const char *end = str + len;
if (p >= end) {
return 0;
}
if (!isdigit(*p)) {
return 0;
}
int v = 0;
while (p < end) {
v *= 10;
v += *p - '0';
++p;
if (!isdigit(*p)) {
break;
}
}
value = v;
return p - str;
}
int skip_comma(const char *str, const size_t len)
{
const char *p = str;
const char *end = str + len;
if (p >= end) {
return 0;
}
return *p == ',' ? 1 : 0;
}
class CSVTest : public CppUnit::TestCase
{
CPPUNIT_TEST_SUITE(CSVTest);
CPPUNIT_TEST(test_parse_integer);
CPPUNIT_TEST(test_skip_comma);
CPPUNIT_TEST_SUITE_END();
public:
void test_parse_integer();
void test_skip_comma();
};
void CSVTest::test_parse_integer()
{
int i;
CPPUNIT_ASSERT_EQUAL(0, parse_integer(i, "", 0));
CPPUNIT_ASSERT_EQUAL(1, parse_integer(i, "7", 1));
CPPUNIT_ASSERT_EQUAL(7, i);
CPPUNIT_ASSERT_EQUAL(3, parse_integer(i, "765", 3));
CPPUNIT_ASSERT_EQUAL(765, i);
CPPUNIT_ASSERT_EQUAL(1, parse_integer(i, "7,65", 4));
CPPUNIT_ASSERT_EQUAL(7, i);
}
void CSVTest::test_skip_comma()
{
CPPUNIT_ASSERT_EQUAL(0, skip_comma("", 0));
CPPUNIT_ASSERT_EQUAL(1, skip_comma(",", 1));
CPPUNIT_ASSERT_EQUAL(0, skip_comma("0,", 2));
}
CPPUNIT_TEST_SUITE_REGISTRATION(CSVTest);