-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCollatz.c++
222 lines (172 loc) · 5.2 KB
/
TestCollatz.c++
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// --------------------------------
// projects/collatz/TestCollatz.c++
// Copyright (C) 2014
// Glenn P. Downing
// --------------------------------
// https://code.google.com/p/googletest/wiki/V1_7_Primer#Basic_Assertions
// --------
// includes
// --------
#include <iostream> // cout, endl
#include <sstream> // istringtstream, ostringstream
#include <string> // ==
#include <utility> // pair
#include "gtest/gtest.h"
#include "Collatz.h"
// -----------
// TestCollatz
// -----------
// ----
// read
// ----
TEST(Collatz, read) {
std::istringstream r("1 10\n");
const std::pair<int, int> p = collatz_read(r);
ASSERT_EQ( 1, p.first);
ASSERT_EQ(10, p.second);}
TEST(Collatz, read_2) {
std::istringstream r("1000000 1\n");
const std::pair<int, int> p = collatz_read(r);
ASSERT_EQ( 1000000, p.first);
ASSERT_EQ( 1, p.second);}
TEST(Collatz, read_3) {
std::istringstream r("10 1\n");
const std::pair<int, int> p = collatz_read(r);
ASSERT_EQ( 10, p.first);
ASSERT_EQ(1, p.second);}
// ----
// eval
// ----
TEST(Collatz, eval_1) {
const int v = collatz_eval(1, 10);
ASSERT_EQ(20, v);}
TEST(Collatz, eval_2) {
const int v = collatz_eval(100, 200);
ASSERT_EQ(125, v);}
TEST(Collatz, eval_3) {
const int v = collatz_eval(201, 210);
ASSERT_EQ(89, v);}
TEST(Collatz, eval_4) {
const int v = collatz_eval(900, 1000);
ASSERT_EQ(174, v);}
TEST(Collatz, eval_5) {
const int v = collatz_eval(300000, 291000);
ASSERT_EQ(371, v);}
// ----
// cycle_length
// ----
TEST(Collatz, cycle_1) {
const int v = collatz_cycle_length(1);
ASSERT_EQ(1, v);}
TEST(Collatz, cycle_2) {
const int v = collatz_cycle_length(500000);
ASSERT_EQ(152, v);}
TEST(Collatz, cycle_3) {
const int v = collatz_cycle_length(6999);
ASSERT_EQ(133, v);}
TEST(Collatz, cycle_4) {
const int v = collatz_cycle_length(105019);
ASSERT_EQ(80, v);}
// -----
// print
// -----
TEST(Collatz, print) {
std::ostringstream w;
collatz_print(w, 1, 10, 20);
ASSERT_EQ("1 10 20\n", w.str());}
TEST(Collatz, print_2) {
std::ostringstream w;
collatz_print(w, 10, 10, 7);
ASSERT_EQ("10 10 7\n", w.str());}
TEST(Collatz, print_3) {
std::ostringstream w;
collatz_print(w, 10, 1, 20);
ASSERT_EQ("10 1 20\n", w.str());}
// -----
// solve
// -----
TEST(Collatz, solve) {
std::istringstream r("1 10\n100 200\n201 210\n900 1000\n");
std::ostringstream w;
collatz_solve(r, w);
ASSERT_EQ("1 10 20\n100 200 125\n201 210 89\n900 1000 174\n", w.str());}
TEST(Collatz, solve_2) {
std::istringstream r("10 10\n100000 200000\n500000 555555\n");
std::ostringstream w;
collatz_solve(r, w);
ASSERT_EQ("10 10 7\n100000 200000 383\n500000 555555 470\n", w.str());}
TEST(Collatz, solve_3) {
std::istringstream r("10 10\n100000 110000\n9999 99999\n");
std::ostringstream w;
collatz_solve(r, w);
ASSERT_EQ("10 10 7\n100000 110000 354\n9999 99999 351\n", w.str());}
/*
% ls -al /usr/include/gtest/
...
gtest.h
...
% locate libgtest.a
/usr/lib/libgtest.a
...
% locate libpthread.a
...
/usr/lib32/libpthread.a
% locate libgtest_main.a
/usr/lib/libgtest_main.a
...
% g++-4.7 -fprofile-arcs -ftest-coverage -pedantic -std=c++11 -Wall Collatz.c++ TestCollatz.c++ -o TestCollatz -lgtest -lgtest_main -lpthread
% valgrind TestCollatz > TestCollatz.out 2>&1
% gcov-4.7 -b Collatz.c++ >> TestCollatz.out
% gcov-4.7 -b TestCollatz.c++ >> TestCollatz.out
% cat TestCollatz.out
==14225== Memcheck, a memory error detector
==14225== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==14225== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==14225== Command: TestCollatz
==14225==
Running main() from gtest_main.cc
[==========] Running 7 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 7 tests from Collatz
[ RUN ] Collatz.read
[ OK ] Collatz.read (31 ms)
[ RUN ] Collatz.eval_1
[ OK ] Collatz.eval_1 (5 ms)
[ RUN ] Collatz.eval_2
[ OK ] Collatz.eval_2 (3 ms)
[ RUN ] Collatz.eval_3
[ OK ] Collatz.eval_3 (3 ms)
[ RUN ] Collatz.eval_4
[ OK ] Collatz.eval_4 (3 ms)
[ RUN ] Collatz.print
[ OK ] Collatz.print (17 ms)
[ RUN ] Collatz.solve
[ OK ] Collatz.solve (10 ms)
[----------] 7 tests from Collatz (88 ms total)
[----------] Global test environment tear-down
[==========] 7 tests from 1 test case ran. (132 ms total)
[ PASSED ] 7 tests.
==14225==
==14225== HEAP SUMMARY:
==14225== in use at exit: 0 bytes in 0 blocks
==14225== total heap usage: 495 allocs, 495 frees, 70,302 bytes allocated
==14225==
==14225== All heap blocks were freed -- no leaks are possible
==14225==
==14225== For counts of detected and suppressed errors, rerun with: -v
==14225== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
File 'Collatz.c++'
Lines executed:100.00% of 17
Branches executed:100.00% of 18
Taken at least once:61.11% of 18
Calls executed:89.47% of 19
Creating 'Collatz.c++.gcov'
...
File 'TestCollatz.c++'
Lines executed:100.00% of 26
Branches executed:57.14% of 224
Taken at least once:28.57% of 224
Calls executed:54.07% of 209
Creating 'TestCollatz.c++.gcov'
...
*/