Skip to content

Instantly share code, notes, and snippets.

@thoni56
Last active November 15, 2016 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoni56/6ba7b78b14aee0fc4015b4ab40f40e73 to your computer and use it in GitHub Desktop.
Save thoni56/6ba7b78b14aee0fc4015b4ab40f40e73 to your computer and use it in GitHub Desktop.
Cgreen floating point mock test
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
double foobar(void) {return (double)mock();}
double test_func(void)
{
return foobar();
}
Ensure(foobar_returns_floating_point_from_literal)
{
// Mock actually returns integer "282"
expect(foobar, will_return(282.4));
assert_that_double(test_func(), is_equal_to_double(282.4));
}
Ensure(foobar_returns_floating_point_from_variable)
{
double return_value=282.4;
// Mock actually returns integer "282"
expect(foobar, will_return(return_value));
assert_that_double(test_func(), is_equal_to_double(282.4));
}
uint8_t foobar_param(const double bob) {return (uint8_t)mock(bob);}
uint8_t test_func_param(void)
{
return foobar_param(17.1);
}
Ensure(foobar_param_doesnt_segfault)
{
// Mock segfaults for floating point parameter?
expect(foobar_param, will_return(7U), when(bob, is_equal_to_double(17.1)));
assert_that(test_func_param(), is_equal_to(7U));
}
int main(int argc, char **argv)
{
TestSuite *suite = create_named_test_suite("floating point mock test");
add_test(suite, foobar_returns_floating_point_from_literal);
add_test(suite, foobar_param_doesnt_segfault);
return run_test_suite(suite, create_text_reporter());
}
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
double foobar(void) {return (double)unbox_double(mock());}
double test_func(void)
{
return foobar();
}
Ensure(foobar_returns_floating_point)
{
// Mock actually returns integer "282"
expect(foobar, will_return(box_double(282.4)));
assert_that_double(test_func(), is_equal_to_double(282.4));
}
uint8_t foobar_param(const double bob) {return (uint8_t)mock(box_double(bob));}
uint8_t test_func_param(void)
{
return foobar_param(17.1);
}
Ensure(foobar_param_doesnt_segfault)
{
// Mock segfaults for floating point parameter?
expect(foobar_param, will_return(7U), when(bob, is_equal_to_double(17.1)));
assert_that(test_func_param(), is_equal_to(7U));
}
int main(int argc, char **argv)
{
TestSuite *suite = create_named_test_suite("floating point mock test");
add_test(suite, foobar_returns_floating_point);
add_test(suite, foobar_param_doesnt_segfault);
return run_test_suite(suite, create_text_reporter());
}
CFLAGS=-I/usr/local/include -g -O0
all: 96
LD_LIBRARY_PATH=/usr/local/lib ./96
96: 96.o
gcc -o $@ $^ -L/usr/local/lib -lcgreen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment