Skip to content

Commit 2efbe72

Browse files
committed
Merge branch 'feature/switch_to_gtest' into develop
2 parents 485f64b + 8b4e8a7 commit 2efbe72

File tree

8 files changed

+119
-128
lines changed

8 files changed

+119
-128
lines changed

.gitignore

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1+
#######
2+
# C/C++
3+
#######
4+
5+
# Prerequisites
6+
*.d
7+
18
# Object files
29
*.o
10+
*.ko
11+
*.obj
12+
*.elf
13+
14+
# Precompiled Headers
15+
*.gch
16+
*.pch
317

418
# Libraries
519
*.lib
620
*.a
7-
!lib/libcmockery_la-cmockery.o
21+
*.la
22+
*.lo
823

924
# Shared objects (inc. Windows DLLs)
1025
*.dll
@@ -16,3 +31,16 @@
1631
*.exe
1732
*.out
1833
*.app
34+
*.i*86
35+
*.x86_64
36+
*.hex
37+
38+
# Debug files
39+
*.dSYM/
40+
*.su
41+
42+
#######
43+
# emacs
44+
#######
45+
46+
*~

README.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ The basic structure for each project is (for an imaginary project
7878
want to include them in the tests.
7979
- `foo.c`, which includes the initial stub (or an incorrect version)
8080
of the program you're working with in that part.
81-
- `main.c`, which gives you a "main" function that you can use to
82-
run your code separate from the test code. You don't have to ever
83-
do this, but you might find it useful in debugging.
8481
- `foo_test.cpp`, which is the test file we wrote using `gtest`. The
8582
`.cpp` ending is because this is actually a C++ file not a strict
8683
C file. That will affect how you compile the test code, but you
@@ -89,16 +86,6 @@ The basic structure for each project is (for an imaginary project
8986
Your job then is typically to complete or fix `foo.c`, which provides
9087
the implementation of the function listed in `foo.h`.
9188

92-
To compile the `main` use the following:
93-
94-
```bash
95-
gcc -Wall -g -o foo foo.c main.c
96-
```
97-
98-
(where you replace `foo` with the appropriate name for the project
99-
you're working on). If all goes well, that should generate an executable
100-
`foo` that you can run with `./foo`.
101-
10289
To compile the test code use the following:
10390

10491
```bash

array_merge/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore the generated test executable
2+
array_merge_test

array_merge/array_merge.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef ARRAY_MERGE_H_GUARD
22
#define ARRAY_MERGE_H_GUARD
33

4-
#include <stdbool.h>
5-
64
#define UNIT_TESTING
75

86
int* array_merge(int num_arrays, int* sizes, int** values);
Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
1-
#include <stdarg.h>
2-
#include <setjmp.h>
3-
#include <stdlib.h>
1+
#include <gtest/gtest.h>
42

5-
#include "../include/cmockery.h"
63
#include "array_merge.h"
74

8-
bool arrays_match(int size, int a[], int b[]) {
5+
void arrays_match(int size, int a[], int b[]) {
96
int i;
107

118
for (i=0; i<size; ++i) {
12-
assert_int_equal(a[i], b[i]);
9+
ASSERT_EQ(b[i], a[i]);
1310
}
14-
15-
return true;
1611
}
1712

18-
void test_empty_list() {
13+
TEST(ArrayMerge, Handle_empty_list) {
1914
int* a[] = { };
2015
int sizes[] = { };
2116
int expected[] = { 0 };
2217
int* result;
2318

2419
result = array_merge(0, sizes, a);
25-
assert_true(arrays_match(1, result, expected));
20+
arrays_match(1, result, expected);
2621
}
2722

28-
void test_singleton_list() {
23+
TEST(ArrayMerge, Handle_singleton_list) {
2924
int num_arrays = 1;
3025
int sizes[] = { 1 };
3126
int a0[] = { 5 };
@@ -34,10 +29,10 @@ void test_singleton_list() {
3429
int* result;
3530

3631
result = array_merge(num_arrays, sizes, a);
37-
assert_true(arrays_match(2, result, expected));
32+
arrays_match(2, result, expected);
3833
}
3934

40-
void test_one_longer_list() {
35+
TEST(ArrayMerge, Handle_one_longer_list) {
4136
int num_arrays = 1;
4237
int sizes[] = { 10 };
4338
int a0[] = { 3, 2, 0, 5, 8, 9, 6, 3, 2, 0 };
@@ -46,10 +41,10 @@ void test_one_longer_list() {
4641
int* result;
4742

4843
result = array_merge(num_arrays, sizes, a);
49-
assert_true(arrays_match(8, result, expected));
44+
arrays_match(8, result, expected);
5045
}
5146

52-
void test_multiple_copies_of_longer_list() {
47+
TEST(ArrayMerge, Handle_multiple_copies_of_longer_list) {
5348
int num_arrays = 10;
5449
int sizes[] = { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };
5550
int a0[] = { 3, 2, 0, 5, 8, 9, 6, 3, 2, 0 };
@@ -58,10 +53,10 @@ void test_multiple_copies_of_longer_list() {
5853
int* result;
5954

6055
result = array_merge(num_arrays, sizes, a);
61-
assert_true(arrays_match(8, result, expected));
56+
arrays_match(8, result, expected);
6257
}
6358

64-
void test_multiple_copies_of_longer_list_different_orders() {
59+
TEST(ArrayMerge, Handle_multiple_copies_of_longer_list_different_orders) {
6560
int num_arrays = 9;
6661
int sizes[] = { 10, 10, 10, 10, 10, 10, 10, 10, 10 };
6762
int a0[] = { 3, 2, 0, 5, 8, 9, 6, 3, 2, 0 };
@@ -72,10 +67,10 @@ void test_multiple_copies_of_longer_list_different_orders() {
7267
int* result;
7368

7469
result = array_merge(num_arrays, sizes, a);
75-
assert_true(arrays_match(8, result, expected));
70+
arrays_match(8, result, expected);
7671
}
7772

78-
void test_different_sizes() {
73+
TEST(ArrayMerge, Handle_different_sizes) {
7974
int num_arrays = 11;
8075
int sizes[num_arrays];
8176
int* a[num_arrays];
@@ -85,17 +80,17 @@ void test_different_sizes() {
8580

8681
for (i=0; i<num_arrays; ++i) {
8782
sizes[i] = i;
88-
a[i] = calloc(i, sizeof(int));
83+
a[i] = (int*) calloc(i, sizeof(int));
8984
for (j=0; j<i; ++j) {
9085
a[i][j] = j;
9186
}
9287
}
9388

9489
result = array_merge(num_arrays, sizes, a);
95-
assert_true(arrays_match(11, result, expected));
90+
arrays_match(11, result, expected);
9691
}
9792

98-
void test_different_sizes_reversed() {
93+
TEST(ArrayMerge, Handle_different_sizes_reversed) {
9994
int num_arrays = 11;
10095
int sizes[num_arrays];
10196
int* a[num_arrays];
@@ -105,25 +100,17 @@ void test_different_sizes_reversed() {
105100

106101
for (i=num_arrays-1; i>=0; --i) {
107102
sizes[i] = i;
108-
a[i] = calloc(i, sizeof(int));
103+
a[i] = (int*) calloc(i, sizeof(int));
109104
for (j=0; j<i; ++j) {
110105
a[i][j] = j;
111106
}
112107
}
113108

114109
result = array_merge(num_arrays, sizes, a);
115-
assert_true(arrays_match(11, result, expected));
110+
arrays_match(11, result, expected);
116111
}
117112

118113
int main(int argc, char* argv[]) {
119-
UnitTest tests[] = {
120-
unit_test(test_empty_list),
121-
unit_test(test_singleton_list),
122-
unit_test(test_one_longer_list),
123-
unit_test(test_multiple_copies_of_longer_list),
124-
unit_test(test_multiple_copies_of_longer_list_different_orders),
125-
unit_test(test_different_sizes),
126-
unit_test(test_different_sizes_reversed),
127-
};
128-
return run_tests(tests);
114+
::testing::InitGoogleTest(&argc, argv);
115+
return RUN_ALL_TESTS();
129116
}

mergesort/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore the generated binary
2+
mergesort_test

mergesort/mergesort_test.c

Lines changed: 0 additions & 78 deletions
This file was deleted.

mergesort/mergesort_test.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "mergesort.h"
4+
5+
void arrays_match(int size, int a[], int b[]) {
6+
int i;
7+
8+
for (i=0; i<size; ++i) {
9+
ASSERT_EQ(a[i], b[i]);
10+
}
11+
}
12+
13+
TEST(Mergesort, HandleEmptyList) {
14+
int a[] = { };
15+
int b[] = { };
16+
mergesort(0, b);
17+
arrays_match(0, a, b);
18+
}
19+
20+
TEST(Mergesort, HandleSingletonList) {
21+
int a[] = { 5 };
22+
int b[] = { 5 };
23+
mergesort(1, b);
24+
arrays_match(1, a, b);
25+
}
26+
27+
TEST(Mergesort, HandleOrderedPair) {
28+
int a[] = { 5, 8 };
29+
int expected[] = { 5, 8 };
30+
mergesort(2, a);
31+
arrays_match(2, a, expected);
32+
}
33+
34+
TEST(Mergesort, HandleUnorderedPair) {
35+
int a[] = { 8, 5 };
36+
int expected[] = { 5, 8 };
37+
mergesort(2, a);
38+
arrays_match(2, a, expected);
39+
}
40+
41+
TEST(Mergesort, HandleMixed) {
42+
int a[] = { 5, 8, 9, 6, 3, 2, 0 };
43+
int expected[] = { 0, 2, 3, 5, 6, 8, 9 };
44+
mergesort(7, a);
45+
arrays_match(7, a, expected);
46+
}
47+
48+
TEST(Mergesort, HandleOrdered) {
49+
int a[] = { 0, 2, 3, 5, 6, 8, 9 };
50+
int expected[] = { 0, 2, 3, 5, 6, 8, 9 };
51+
mergesort(7, a);
52+
arrays_match(7, a, expected);
53+
}
54+
55+
TEST(Mergesort, HandleReversed) {
56+
int a[] = { 9, 8, 6, 5, 3, 2, 0 };
57+
int expected[] = { 0, 2, 3, 5, 6, 8, 9 };
58+
mergesort(7, a);
59+
arrays_match(7, a, expected);
60+
}
61+
62+
int main(int argc, char* argv[]) {
63+
::testing::InitGoogleTest(&argc, argv);
64+
return RUN_ALL_TESTS();
65+
}

0 commit comments

Comments
 (0)