Last active
November 28, 2018 07:18
-
-
Save hosackm/0f20417f2c43d47487ff4649a65312a2 to your computer and use it in GitHub Desktop.
Introduction to googletest for C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "adder.h" | |
int add(int a, int b) | |
{ | |
return a + b; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
int add(int a, int b); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "adder.h" | |
#include <stdio.h> | |
int main(int argc, char *argv[]) | |
{ | |
int a = 1, b = 2; | |
fprintf(stdout, "%d + %d = %d\n", a, b, add(a, b)); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CC=clang | |
CCX=clang++ | |
SOURCES=adder.c | |
TESTSOURCES=test_add_two_numbers.cxx run_all_tests.cxx | |
INCLUDES=-I. | |
EXE=adder | |
$(EXE): $(SOURCES) main.c | |
$(CC) $(INCLUDES) $^ -o $@ | |
adder.o: adder.c | |
$(CC) $(INCLUDES) -c $^ -o $@ | |
runtests: adder.o $(TESTSOURCES) | |
$(CCX) -std=c++11 -lgtest $(INCLUDES) $^ -o $@ | |
./$@ | |
clean: | |
rm -rf adder.o runtests $(EXE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gtest/gtest.h> | |
int main(int argc, char *argv[]) | |
{ | |
::testing::InitGoogleTest(&argc, argv); | |
return RUN_ALL_TESTS(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gtest/gtest.h> | |
extern "C" | |
{ | |
#include "adder.h" | |
} | |
TEST(SimpleAdds, OnePlusOneIsTwo) | |
{ | |
EXPECT_EQ(2, add(1, 1)); | |
} | |
TEST(SimpleAdds, NegativePlusPostiveDoesntOverflow) | |
{ | |
EXPECT_EQ(0, add(-1, 1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment