Skip to content

Commit

Permalink
Merge branch 'feature/switch_to_gtest' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
NicMcPhee committed Sep 27, 2016
2 parents 485f64b + 8b4e8a7 commit 2efbe72
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 128 deletions.
30 changes: 29 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
#######
# C/C++
#######

# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
!lib/libcmockery_la-cmockery.o
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
Expand All @@ -16,3 +31,16 @@
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su

#######
# emacs
#######

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

To compile the `main` use the following:

```bash
gcc -Wall -g -o foo foo.c main.c
```

(where you replace `foo` with the appropriate name for the project
you're working on). If all goes well, that should generate an executable
`foo` that you can run with `./foo`.

To compile the test code use the following:

```bash
Expand Down
2 changes: 2 additions & 0 deletions array_merge/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore the generated test executable
array_merge_test
2 changes: 0 additions & 2 deletions array_merge/array_merge.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef ARRAY_MERGE_H_GUARD
#define ARRAY_MERGE_H_GUARD

#include <stdbool.h>

#define UNIT_TESTING

int* array_merge(int num_arrays, int* sizes, int** values);
Expand Down
55 changes: 21 additions & 34 deletions array_merge/array_merge_test.c → array_merge/array_merge_test.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
#include <stdarg.h>
#include <setjmp.h>
#include <stdlib.h>
#include <gtest/gtest.h>

#include "../include/cmockery.h"
#include "array_merge.h"

bool arrays_match(int size, int a[], int b[]) {
void arrays_match(int size, int a[], int b[]) {
int i;

for (i=0; i<size; ++i) {
assert_int_equal(a[i], b[i]);
ASSERT_EQ(b[i], a[i]);
}

return true;
}

void test_empty_list() {
TEST(ArrayMerge, Handle_empty_list) {
int* a[] = { };
int sizes[] = { };
int expected[] = { 0 };
int* result;

result = array_merge(0, sizes, a);
assert_true(arrays_match(1, result, expected));
arrays_match(1, result, expected);
}

void test_singleton_list() {
TEST(ArrayMerge, Handle_singleton_list) {
int num_arrays = 1;
int sizes[] = { 1 };
int a0[] = { 5 };
Expand All @@ -34,10 +29,10 @@ void test_singleton_list() {
int* result;

result = array_merge(num_arrays, sizes, a);
assert_true(arrays_match(2, result, expected));
arrays_match(2, result, expected);
}

void test_one_longer_list() {
TEST(ArrayMerge, Handle_one_longer_list) {
int num_arrays = 1;
int sizes[] = { 10 };
int a0[] = { 3, 2, 0, 5, 8, 9, 6, 3, 2, 0 };
Expand All @@ -46,10 +41,10 @@ void test_one_longer_list() {
int* result;

result = array_merge(num_arrays, sizes, a);
assert_true(arrays_match(8, result, expected));
arrays_match(8, result, expected);
}

void test_multiple_copies_of_longer_list() {
TEST(ArrayMerge, Handle_multiple_copies_of_longer_list) {
int num_arrays = 10;
int sizes[] = { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };
int a0[] = { 3, 2, 0, 5, 8, 9, 6, 3, 2, 0 };
Expand All @@ -58,10 +53,10 @@ void test_multiple_copies_of_longer_list() {
int* result;

result = array_merge(num_arrays, sizes, a);
assert_true(arrays_match(8, result, expected));
arrays_match(8, result, expected);
}

void test_multiple_copies_of_longer_list_different_orders() {
TEST(ArrayMerge, Handle_multiple_copies_of_longer_list_different_orders) {
int num_arrays = 9;
int sizes[] = { 10, 10, 10, 10, 10, 10, 10, 10, 10 };
int a0[] = { 3, 2, 0, 5, 8, 9, 6, 3, 2, 0 };
Expand All @@ -72,10 +67,10 @@ void test_multiple_copies_of_longer_list_different_orders() {
int* result;

result = array_merge(num_arrays, sizes, a);
assert_true(arrays_match(8, result, expected));
arrays_match(8, result, expected);
}

void test_different_sizes() {
TEST(ArrayMerge, Handle_different_sizes) {
int num_arrays = 11;
int sizes[num_arrays];
int* a[num_arrays];
Expand All @@ -85,17 +80,17 @@ void test_different_sizes() {

for (i=0; i<num_arrays; ++i) {
sizes[i] = i;
a[i] = calloc(i, sizeof(int));
a[i] = (int*) calloc(i, sizeof(int));
for (j=0; j<i; ++j) {
a[i][j] = j;
}
}

result = array_merge(num_arrays, sizes, a);
assert_true(arrays_match(11, result, expected));
arrays_match(11, result, expected);
}

void test_different_sizes_reversed() {
TEST(ArrayMerge, Handle_different_sizes_reversed) {
int num_arrays = 11;
int sizes[num_arrays];
int* a[num_arrays];
Expand All @@ -105,25 +100,17 @@ void test_different_sizes_reversed() {

for (i=num_arrays-1; i>=0; --i) {
sizes[i] = i;
a[i] = calloc(i, sizeof(int));
a[i] = (int*) calloc(i, sizeof(int));
for (j=0; j<i; ++j) {
a[i][j] = j;
}
}

result = array_merge(num_arrays, sizes, a);
assert_true(arrays_match(11, result, expected));
arrays_match(11, result, expected);
}

int main(int argc, char* argv[]) {
UnitTest tests[] = {
unit_test(test_empty_list),
unit_test(test_singleton_list),
unit_test(test_one_longer_list),
unit_test(test_multiple_copies_of_longer_list),
unit_test(test_multiple_copies_of_longer_list_different_orders),
unit_test(test_different_sizes),
unit_test(test_different_sizes_reversed),
};
return run_tests(tests);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
2 changes: 2 additions & 0 deletions mergesort/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore the generated binary
mergesort_test
78 changes: 0 additions & 78 deletions mergesort/mergesort_test.c

This file was deleted.

65 changes: 65 additions & 0 deletions mergesort/mergesort_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <gtest/gtest.h>

#include "mergesort.h"

void arrays_match(int size, int a[], int b[]) {
int i;

for (i=0; i<size; ++i) {
ASSERT_EQ(a[i], b[i]);
}
}

TEST(Mergesort, HandleEmptyList) {
int a[] = { };
int b[] = { };
mergesort(0, b);
arrays_match(0, a, b);
}

TEST(Mergesort, HandleSingletonList) {
int a[] = { 5 };
int b[] = { 5 };
mergesort(1, b);
arrays_match(1, a, b);
}

TEST(Mergesort, HandleOrderedPair) {
int a[] = { 5, 8 };
int expected[] = { 5, 8 };
mergesort(2, a);
arrays_match(2, a, expected);
}

TEST(Mergesort, HandleUnorderedPair) {
int a[] = { 8, 5 };
int expected[] = { 5, 8 };
mergesort(2, a);
arrays_match(2, a, expected);
}

TEST(Mergesort, HandleMixed) {
int a[] = { 5, 8, 9, 6, 3, 2, 0 };
int expected[] = { 0, 2, 3, 5, 6, 8, 9 };
mergesort(7, a);
arrays_match(7, a, expected);
}

TEST(Mergesort, HandleOrdered) {
int a[] = { 0, 2, 3, 5, 6, 8, 9 };
int expected[] = { 0, 2, 3, 5, 6, 8, 9 };
mergesort(7, a);
arrays_match(7, a, expected);
}

TEST(Mergesort, HandleReversed) {
int a[] = { 9, 8, 6, 5, 3, 2, 0 };
int expected[] = { 0, 2, 3, 5, 6, 8, 9 };
mergesort(7, a);
arrays_match(7, a, expected);
}

int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 2efbe72

Please sign in to comment.