Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks for whether a string is in another string #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ install-sh
ltmain.sh
m4/
missing
src/cmockery/config.h.in*
test-driver
*.sublime-*
*.sublime-*

# some build artifacts... why aren't these ignored?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I'm not sure. Thank you for setting this up

/src/cmockery/config.h*
/src/cmockery/stamp-h1
/*_test
/*.o
/.deps/
/.libs/
/Makefile
/calculator
/cmockery2.pc
/config.log
/config.status
/libcmockery.la
/libcmockery_la-cmockery.lo
/libtool
/run_tests
106 changes: 93 additions & 13 deletions src/cmockery.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ static int integer_not_in_range_display_error(
if (value < range_min || value > range_max) {
return 1;
}
print_error("%" PRIuMAX " is within the range %" PRIuMAX "-%" PRIuMAX "\n",
print_error("%" PRIuMAX " is within the range %" PRIuMAX "-%" PRIuMAX "\n",
value, range_min, range_max);
return 0;
}
Expand Down Expand Up @@ -886,6 +886,31 @@ static int string_not_equal_display_error(
}


/* Determine whether needle is within haystack. If needle is found
* 1 is returned. If needle is not found an error is displayed and 0 is
* returned. */
static int value_in_string_display_error(
const char * const haystack, const char * const needle) {
if (strstr(haystack, needle)) {
return 1;
}
print_error("\"%s\" not found in \"%s\"\n", needle, haystack);
return 0;
}

/* Determine whether needle is within haystack. If needle is not found
* 1 is returned. If needle is found an error is displayed and 0 is
* returned. */
static int value_not_in_string_display_error(
const char * const haystack, const char * const needle) {
if (!strstr(haystack, needle)) {
return 1;
}
print_error("\"%s\" found in \"%s\"\n", needle, haystack);
return 0;
}


/* Determine whether the specified areas of memory are equal. If they're equal
* 1 is returned otherwise an error is displayed and 0 is returned. */
static int memory_equal_display_error(const char* const a, const char* const b,
Expand All @@ -896,15 +921,15 @@ static int memory_equal_display_error(const char* const a, const char* const b,
const char l = a[i];
const char r = b[i];
if (l != r) {
print_error("difference at offset %" PRIuMAX " 0x%02x 0x%02x\n",
print_error("difference at offset %" PRIuMAX " 0x%02x 0x%02x\n",
cast_to_largest_integral_type(i), l, r);
differences ++;
}
}
if (differences) {
print_error("%d bytes of 0x%08" PRIxMAX " and 0x%08" PRIxMAX " differ\n",
print_error("%d bytes of 0x%08" PRIxMAX " and 0x%08" PRIxMAX " differ\n",
differences,
cast_ptr_to_largest_integral_type(a),
cast_ptr_to_largest_integral_type(a),
cast_ptr_to_largest_integral_type(b));
return 0;
}
Expand All @@ -927,9 +952,9 @@ static int memory_not_equal_display_error(
}
}
if (same == size) {
print_error("%" PRIuMAX " bytes of 0x%08" PRIxMAX " and 0x%08" PRIxMAX" the same\n",
print_error("%" PRIuMAX " bytes of 0x%08" PRIxMAX " and 0x%08" PRIxMAX" the same\n",
cast_to_largest_integral_type(same),
cast_ptr_to_largest_integral_type(a),
cast_ptr_to_largest_integral_type(a),
cast_ptr_to_largest_integral_type(b));
return 0;
}
Expand Down Expand Up @@ -1064,6 +1089,47 @@ void _expect_not_in_range(
}


// CheckParameterValue callback to check whether a parameter is in a string.
static int check_in_string(const uintmax_t value,
const uintmax_t check_value_data) {
return value_in_string_display_error(
(char *)((uintptr_t)value),
cast_largest_integral_type_to_pointer(char*, check_value_data));
}


// Add an event to check whether a parameter is in a string.
void _expect_in_string(
const char* const function, const char* const parameter,
const char* const file, const int line, const char* string,
const int count) {
declare_initialize_value_pointer_pointer(string_pointer, (char*)string);
_expect_check(function, parameter, file, line, check_in_string,
string_pointer.value, NULL, count);
}


/* CheckParameterValue callback to check whether a parameter is in
* a string. */
static int check_not_in_string(const uintmax_t value,
const uintmax_t check_value_data) {
return value_not_in_string_display_error(
(char *)((uintptr_t)value),
cast_largest_integral_type_to_pointer(char*, check_value_data));
}


// Add an event to check whether a parameter is not in a string.
void _expect_not_in_string(
const char* const function, const char* const parameter,
const char* const file, const int line, const char* string,
const int count) {
declare_initialize_value_pointer_pointer(string_pointer, (char*)string);
_expect_check(function, parameter, file, line, check_not_in_string,
string_pointer.value, NULL, count);
}


/* CheckParameterValue callback to check whether a value is equal to an
* expected value. */
static int check_value(const uintmax_t value,
Expand Down Expand Up @@ -1387,6 +1453,20 @@ void _assert_not_in_set(const uintmax_t value,
}
}

void _assert_in_string(const char * const a, const char * const b,
const char * const file, const int line) {
if (!value_in_string_display_error(a, b)) {
_fail(file, line);
}
}

void _assert_not_in_string(const char * const a, const char * const b,
const char * const file, const int line) {
if (!value_not_in_string_display_error(a, b)) {
_fail(file, line);
}
}


// Get the list of allocated blocks.
static ListNode* get_allocated_blocks_list() {
Expand Down Expand Up @@ -1463,7 +1543,7 @@ void _test_free(void* const ptr, const char* file, const int line) {
print_error(
"Guard block of 0x%08" PRIxMAX " size=%" PRIuMAX " allocated by "
SOURCE_LOCATION_FORMAT " at 0x%08" PRIxMAX " is corrupt\n",
cast_ptr_to_largest_integral_type(ptr),
cast_ptr_to_largest_integral_type(ptr),
cast_to_largest_integral_type(block_info->size),
block_info->location.file, block_info->location.line,
cast_ptr_to_largest_integral_type(&guard[j]));
Expand All @@ -1482,10 +1562,10 @@ void _test_free(void* const ptr, const char* file, const int line) {


// Implement realloc using _test_xalloc functions
void *
_test_realloc(void* ptr,
const size_t size,
const char* file,
void *
_test_realloc(void* ptr,
const size_t size,
const char* file,
const int line)
{

Expand Down Expand Up @@ -1545,7 +1625,7 @@ static int display_allocated_blocks(const ListNode * const check_point) {
print_error("Blocks allocated...\n");
}
print_error(" 0x%08" PRIxMAX " : " SOURCE_LOCATION_FORMAT "\n",
cast_ptr_to_largest_integral_type(block_info->block),
cast_ptr_to_largest_integral_type(block_info->block),
block_info->location.file,
block_info->location.line);
allocated_blocks ++;
Expand Down Expand Up @@ -1745,7 +1825,7 @@ int _run_test(

// Collect time data
gettimeofday(&time_end, NULL);
testcase->time_in_msecs =
testcase->time_in_msecs =
( (time_end.tv_sec-time_start.tv_sec)*1000.0
+ (time_end.tv_usec-time_start.tv_usec)/1000.0 );

Expand Down
39 changes: 37 additions & 2 deletions src/cmockery/cmockery.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
#function, #parameter, __FILE__, __LINE__, value_array, \
sizeof(value_array) / sizeof((value_array)[0]), count)


/* Add an event to check a parameter, using check_expected(), against a
* signed range. Where range is minimum <= value <= maximum.
* See will_return() for a description of the count parameter.
Expand All @@ -179,6 +178,21 @@ cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
_expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
minimum, maximum, count)

/* Add an event to check whether a parameter, using check_expected(),
* is or isn't in a string. See will_return() for a description of the count
* parameter.
*/
#define expect_in_string(function, parameter, string) \
expect_in_string_count(function, parameter, string, 1)
#define expect_in_string_count(function, parameter, string, count) \
_expect_in_string(#function, #parameter, __FILE__, __LINE__, string, \
count)
#define expect_not_in_string(function, parameter, string) \
expect_not_in_string_count(function, parameter, string, 1)
#define expect_not_in_string_count(function, parameter, string, count) \
_expect_not_in_string(#function, #parameter, __FILE__, __LINE__, string, \
count)

/* Add an event to check whether a parameter, using check_expected(), is or
* isn't a value. See will_return() for a description of the count parameter.
*/
Expand Down Expand Up @@ -223,7 +237,6 @@ cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
_expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
(const void*)(memory), size, count)


/* Add an event to allow any value for a parameter checked using
* check_expected(). See will_return() for a description of the count
* parameter.
Expand Down Expand Up @@ -315,6 +328,13 @@ __FILE__, __LINE__)
#define assert_not_in_set(value, values, number_of_values) \
_assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)

// Assert that the specified value is within a string.
#define assert_in_string(value, string) \
_assert_in_string(value, string, __FILE__, __LINE__)
// Assert that the specified value is not within a string.
#define assert_not_in_string(value, string) \
_assert_not_in_set(value, string, __FILE__, __LINE__)


// Forces the test to fail immediately and quit.
#define fail() _fail(__FILE__, __LINE__)
Expand Down Expand Up @@ -475,6 +495,15 @@ void _expect_not_in_range(
const uintmax_t minimum,
const uintmax_t maximum, const int count);

void _expect_in_string(
const char* const function, const char* const parameter,
const char* const file, const int line, const char* string,
const int count);
void _expect_not_in_string(
const char* const function, const char* const parameter,
const char* const file, const int line, const char* string,
const int count);

void _expect_value(
const char* const function, const char* const parameter,
const char* const file, const int line, const uintmax_t value,
Expand Down Expand Up @@ -544,6 +573,12 @@ void _assert_in_set(
void _assert_not_in_set(
const uintmax_t value, const uintmax_t values[],
const size_t number_of_values, const char* const file, const int line);
void _assert_in_string(
const char* const haystack, const char* const needle,
const char* const file, const int line);
void _assert_not_in_string(
const char* const haystack, const char* const needle,
const char* const file, const int line);

void _fail(const char * const file, const int line);

Expand Down