[A] Initial commit
This commit is contained in:
140
tests/cmocka-1.1.0/example/CMakeLists.txt
Normal file
140
tests/cmocka-1.1.0/example/CMakeLists.txt
Normal file
@@ -0,0 +1,140 @@
|
||||
project(cmocka-examples C)
|
||||
|
||||
include_directories(
|
||||
${CMAKE_BINARY_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMOCKA_PUBLIC_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
set_source_files_properties(
|
||||
calculator.c
|
||||
allocate_module.c
|
||||
assert_module.c
|
||||
PROPERTIES
|
||||
COMPILE_DEFINITIONS UNIT_TESTING=1)
|
||||
|
||||
|
||||
if (WIN32 OR CYGWIN OR MINGW)
|
||||
set(CMOCKA_DLL_LIB ${CMAKE_BINARY_DIR}/src)
|
||||
file(TO_NATIVE_PATH "${CMOCKA_DLL_PATH}" CMOCKA_DLL_PATH)
|
||||
set(DLL_PATH_ENV "${CMOCKA_DLL_PATH};$ENV{PATH}")
|
||||
|
||||
#
|
||||
# IMPORTANT NOTE: The set_tests_properties(), below, internally
|
||||
# stores its name/value pairs with a semicolon delimiter.
|
||||
# because of this we must protect the semicolons in the path
|
||||
#
|
||||
string(REPLACE ";" "\\;" DLL_PATH_ENV "${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR CYGWIN OR MINGW)
|
||||
|
||||
|
||||
### The most simple test
|
||||
add_executable(simple_test simple_test.c)
|
||||
target_link_libraries(simple_test ${CMOCKA_SHARED_LIBRARY})
|
||||
|
||||
add_test(simple_test ${CMAKE_CURRENT_BINARY_DIR}/simple_test)
|
||||
if (WIN32 OR CYGWIN OR MINGW)
|
||||
set_tests_properties(simple_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR CYGWIN OR MINGW)
|
||||
|
||||
### Calulator test
|
||||
#TODO investigate dll jmp issue on MinGW
|
||||
if (NOT MINGW OR WITH_STATIC_LIB)
|
||||
add_executable(calculator_test calculator.c calculator_test.c)
|
||||
add_test(calculator_test ${CMAKE_CURRENT_BINARY_DIR}/calculator_test)
|
||||
if (WIN32 OR CYGWIN)
|
||||
set_tests_properties(calculator_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR CYGWIN)
|
||||
|
||||
if (MINGW)
|
||||
target_link_libraries(calculator_test ${CMOCKA_STATIC_LIBRARY})
|
||||
else (MINGW)
|
||||
target_link_libraries(calculator_test ${CMOCKA_SHARED_LIBRARY})
|
||||
endif (MINGW)
|
||||
|
||||
if (WIN32 OR CYGWIN OR MINGW)
|
||||
set_tests_properties(calculator_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR CYGWIN OR MINGW)
|
||||
endif (NOT MINGW OR WITH_STATIC_LIB)
|
||||
|
||||
### Allocate module test
|
||||
add_executable(allocate_module_test allocate_module.c allocate_module_test.c)
|
||||
target_link_libraries(allocate_module_test ${CMOCKA_SHARED_LIBRARY})
|
||||
|
||||
# This is a test that should detect leaks and overflows and will fail for that
|
||||
add_test(allocate_module_test ${CMAKE_CURRENT_BINARY_DIR}/allocate_module_test)
|
||||
if (WIN32 OR CYGWIN OR MINGW)
|
||||
set_tests_properties(allocate_module_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR CYGWIN OR MINGW)
|
||||
|
||||
set_tests_properties(
|
||||
allocate_module_test
|
||||
PROPERTIES
|
||||
WILL_FAIL 1
|
||||
)
|
||||
|
||||
### Assert macro test
|
||||
add_executable(assert_macro_test assert_macro.c assert_macro_test.c)
|
||||
target_link_libraries(assert_macro_test ${CMOCKA_SHARED_LIBRARY})
|
||||
|
||||
add_test(assert_macro_test ${CMAKE_CURRENT_BINARY_DIR}/assert_macro_test)
|
||||
set_tests_properties(
|
||||
assert_macro_test
|
||||
PROPERTIES
|
||||
WILL_FAIL 1
|
||||
)
|
||||
if (WIN32 OR CYGWIN OR MINGW)
|
||||
set_tests_properties(assert_macro_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR CYGWIN OR MINGW)
|
||||
|
||||
### Assert module test
|
||||
add_executable(assert_module_test assert_module.c assert_module_test.c)
|
||||
target_link_libraries(assert_module_test ${CMOCKA_SHARED_LIBRARY})
|
||||
|
||||
add_test(assert_module_test ${CMAKE_CURRENT_BINARY_DIR}/assert_module_test)
|
||||
set_tests_properties(
|
||||
assert_module_test
|
||||
PROPERTIES
|
||||
WILL_FAIL 1
|
||||
)
|
||||
if (WIN32 OR CYGWIN OR MINGW)
|
||||
set_tests_properties(assert_module_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR CYGWIN OR MINGW)
|
||||
|
||||
### Customer database test
|
||||
add_executable(customer_database_test customer_database.c customer_database_test.c)
|
||||
target_link_libraries(customer_database_test ${CMOCKA_SHARED_LIBRARY})
|
||||
|
||||
add_test(customer_database_test ${CMAKE_CURRENT_BINARY_DIR}/customer_database_test)
|
||||
if (WIN32 OR CYGWIN OR MINGW)
|
||||
set_tests_properties(customer_database_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR CYGWIN OR MINGW)
|
||||
|
||||
### Key Value Test
|
||||
add_executable(key_value_test key_value.c key_value_test.c)
|
||||
target_link_libraries(key_value_test ${CMOCKA_SHARED_LIBRARY})
|
||||
|
||||
add_test(key_value_test ${CMAKE_CURRENT_BINARY_DIR}/key_value_test)
|
||||
if (WIN32 OR CYGWIN OR MINGW)
|
||||
set_tests_properties(key_value_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR CYGWIN OR MINGW)
|
||||
|
||||
### Product database test
|
||||
add_executable(product_database_test product_database.c product_database_test.c)
|
||||
target_link_libraries(product_database_test ${CMOCKA_SHARED_LIBRARY})
|
||||
|
||||
add_test(product_database_test ${CMAKE_CURRENT_BINARY_DIR}/product_database_test)
|
||||
set_tests_properties(
|
||||
product_database_test
|
||||
PROPERTIES
|
||||
PASS_REGULAR_EXPRESSION
|
||||
"\\[ FAILED \\] 2 test"
|
||||
)
|
||||
if (WIN32 OR CYGWIN OR MINGW)
|
||||
set_tests_properties(product_database_test PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR CYGWIN OR MINGW)
|
||||
|
||||
# TODO Execute "$CMAKE_LINKER --help" and check for --wrap
|
||||
if (${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)" AND NOT APPLE)
|
||||
add_subdirectory(chef_wrap)
|
||||
endif()
|
||||
55
tests/cmocka-1.1.0/example/allocate_module.c
Normal file
55
tests/cmocka-1.1.0/example/allocate_module.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
#ifdef HAVE_MALLOC_H
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef UNIT_TESTING
|
||||
extern void* _test_malloc(const size_t size, const char* file, const int line);
|
||||
extern void* _test_calloc(const size_t number_of_elements, const size_t size,
|
||||
const char* file, const int line);
|
||||
extern void _test_free(void* const ptr, const char* file, const int line);
|
||||
|
||||
#define malloc(size) _test_malloc(size, __FILE__, __LINE__)
|
||||
#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
|
||||
#define free(ptr) _test_free(ptr, __FILE__, __LINE__)
|
||||
#endif // UNIT_TESTING
|
||||
|
||||
void leak_memory(void);
|
||||
void buffer_overflow(void);
|
||||
void buffer_underflow(void);
|
||||
|
||||
void leak_memory(void) {
|
||||
int * const temporary = (int*)malloc(sizeof(int));
|
||||
*temporary = 0;
|
||||
}
|
||||
|
||||
void buffer_overflow(void) {
|
||||
char * const memory = (char*)malloc(sizeof(int));
|
||||
memory[sizeof(int)] = '!';
|
||||
free(memory);
|
||||
}
|
||||
|
||||
void buffer_underflow(void) {
|
||||
char * const memory = (char*)malloc(sizeof(int));
|
||||
memory[-1] = '!';
|
||||
free(memory);
|
||||
}
|
||||
53
tests/cmocka-1.1.0/example/allocate_module_test.c
Normal file
53
tests/cmocka-1.1.0/example/allocate_module_test.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
extern void leak_memory();
|
||||
extern void buffer_overflow();
|
||||
extern void buffer_underflow();
|
||||
|
||||
/* Test case that fails as leak_memory() leaks a dynamically allocated block. */
|
||||
static void leak_memory_test(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
leak_memory();
|
||||
}
|
||||
|
||||
/* Test case that fails as buffer_overflow() corrupts an allocated block. */
|
||||
static void buffer_overflow_test(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
buffer_overflow();
|
||||
}
|
||||
|
||||
/* Test case that fails as buffer_underflow() corrupts an allocated block. */
|
||||
static void buffer_underflow_test(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
buffer_underflow();
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(leak_memory_test),
|
||||
cmocka_unit_test(buffer_overflow_test),
|
||||
cmocka_unit_test(buffer_underflow_test),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
39
tests/cmocka-1.1.0/example/assert_macro.c
Normal file
39
tests/cmocka-1.1.0/example/assert_macro.c
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "assert_macro.h"
|
||||
|
||||
static const char* status_code_strings[] = {
|
||||
"Address not found",
|
||||
"Connection dropped",
|
||||
"Connection timed out",
|
||||
};
|
||||
|
||||
const char* get_status_code_string(const unsigned int status_code) {
|
||||
return status_code_strings[status_code];
|
||||
}
|
||||
|
||||
unsigned int string_to_status_code(const char* const status_code_string) {
|
||||
unsigned int i;
|
||||
for (i = 0; i < sizeof(status_code_strings) /
|
||||
sizeof(status_code_strings[0]); i++) {
|
||||
if (strcmp(status_code_strings[i], status_code_string) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return ~0U;
|
||||
}
|
||||
2
tests/cmocka-1.1.0/example/assert_macro.h
Normal file
2
tests/cmocka-1.1.0/example/assert_macro.h
Normal file
@@ -0,0 +1,2 @@
|
||||
const char* get_status_code_string(const unsigned int status_code);
|
||||
unsigned int string_to_status_code(const char* const status_code_string);
|
||||
46
tests/cmocka-1.1.0/example/assert_macro_test.c
Normal file
46
tests/cmocka-1.1.0/example/assert_macro_test.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "assert_macro.h"
|
||||
|
||||
/* This test will fail since the string returned by get_status_code_string(0)
|
||||
* doesn't match "Connection timed out". */
|
||||
static void get_status_code_string_test(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_string_equal(get_status_code_string(0), "Address not found");
|
||||
assert_string_equal(get_status_code_string(1), "Connection timed out");
|
||||
}
|
||||
|
||||
/* This test will fail since the status code of "Connection timed out" isn't 1 */
|
||||
static void string_to_status_code_test(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_int_equal(string_to_status_code("Address not found"), 0);
|
||||
assert_int_equal(string_to_status_code("Connection timed out"), 1);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(get_status_code_string_test),
|
||||
cmocka_unit_test(string_to_status_code_test),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
38
tests/cmocka-1.1.0/example/assert_module.c
Normal file
38
tests/cmocka-1.1.0/example/assert_module.c
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <assert.h>
|
||||
|
||||
#include "assert_module.h"
|
||||
|
||||
/* If unit testing is enabled override assert with mock_assert(). */
|
||||
#ifdef UNIT_TESTING
|
||||
extern void mock_assert(const int result, const char* const expression,
|
||||
const char * const file, const int line);
|
||||
#undef assert
|
||||
#define assert(expression) \
|
||||
mock_assert(((expression) ? 1 : 0), #expression, __FILE__, __LINE__);
|
||||
#endif /* UNIT_TESTING */
|
||||
|
||||
void increment_value(int * const value) {
|
||||
assert(value);
|
||||
(*value) ++;
|
||||
}
|
||||
|
||||
void decrement_value(int * const value) {
|
||||
if (value) {
|
||||
(*value) --;
|
||||
}
|
||||
}
|
||||
18
tests/cmocka-1.1.0/example/assert_module.h
Normal file
18
tests/cmocka-1.1.0/example/assert_module.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
void increment_value(int * const value);
|
||||
void decrement_value(int * const value);
|
||||
56
tests/cmocka-1.1.0/example/assert_module_test.c
Normal file
56
tests/cmocka-1.1.0/example/assert_module_test.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "assert_module.h"
|
||||
|
||||
extern void increment_value(int * const value);
|
||||
|
||||
/* This test case will fail but the assert is caught by run_tests() and the
|
||||
* next test is executed. */
|
||||
static void increment_value_fail(void **state) {
|
||||
(void) state;
|
||||
|
||||
increment_value(NULL);
|
||||
}
|
||||
|
||||
/* This test case succeeds since increment_value() asserts on the NULL
|
||||
* pointer. */
|
||||
static void increment_value_assert(void **state) {
|
||||
(void) state;
|
||||
|
||||
expect_assert_failure(increment_value(NULL));
|
||||
}
|
||||
|
||||
/* This test case fails since decrement_value() doesn't assert on a NULL
|
||||
* pointer. */
|
||||
static void decrement_value_fail(void **state) {
|
||||
(void) state;
|
||||
|
||||
expect_assert_failure(decrement_value(NULL));
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(increment_value_fail),
|
||||
cmocka_unit_test(increment_value_assert),
|
||||
cmocka_unit_test(decrement_value_fail),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
286
tests/cmocka-1.1.0/example/calculator.c
Normal file
286
tests/cmocka-1.1.0/example/calculator.c
Normal file
@@ -0,0 +1,286 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* A calculator example used to demonstrate the cmocka testing library. */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#ifdef HAVE_MALLOC_H
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* If this is being built for a unit test. */
|
||||
#ifdef UNIT_TESTING
|
||||
|
||||
/* Redirect printf to a function in the test application so it's possible to
|
||||
* test the standard output. */
|
||||
#ifdef printf
|
||||
#undef printf
|
||||
#endif /* printf */
|
||||
extern int example_test_printf(const char *format, ...);
|
||||
#define printf example_test_printf
|
||||
|
||||
extern void print_message(const char *format, ...);
|
||||
|
||||
/* Redirect fprintf to a function in the test application so it's possible to
|
||||
* test error messages. */
|
||||
#ifdef fprintf
|
||||
#undef fprintf
|
||||
#endif /* fprintf */
|
||||
#define fprintf example_test_fprintf
|
||||
|
||||
extern int example_test_fprintf(FILE * const file, const char *format, ...);
|
||||
|
||||
/* Redirect assert to mock_assert() so assertions can be caught by cmocka. */
|
||||
#ifdef assert
|
||||
#undef assert
|
||||
#endif /* assert */
|
||||
#define assert(expression) \
|
||||
mock_assert((int)(expression), #expression, __FILE__, __LINE__)
|
||||
void mock_assert(const int result, const char* expression, const char *file,
|
||||
const int line);
|
||||
|
||||
/* Redirect calloc and free to test_calloc() and test_free() so cmocka can
|
||||
* check for memory leaks. */
|
||||
#ifdef calloc
|
||||
#undef calloc
|
||||
#endif /* calloc */
|
||||
#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
|
||||
#ifdef free
|
||||
#undef free
|
||||
#endif /* free */
|
||||
#define free(ptr) _test_free(ptr, __FILE__, __LINE__)
|
||||
void* _test_calloc(const size_t number_of_elements, const size_t size,
|
||||
const char* file, const int line);
|
||||
void _test_free(void* const ptr, const char* file, const int line);
|
||||
|
||||
int example_main(int argc, char *argv[]);
|
||||
/* main is defined in the unit test so redefine name of the the main function
|
||||
* here. */
|
||||
#define main example_main
|
||||
|
||||
/* All functions in this object need to be exposed to the test application,
|
||||
* so redefine static to nothing. */
|
||||
#define static
|
||||
|
||||
#endif /* UNIT_TESTING */
|
||||
|
||||
|
||||
/* A binary arithmetic integer operation (add, subtract etc.) */
|
||||
typedef int (*BinaryOperator)(int a, int b);
|
||||
|
||||
/* Structure which maps operator strings to functions. */
|
||||
typedef struct OperatorFunction {
|
||||
const char* operator;
|
||||
BinaryOperator function;
|
||||
} OperatorFunction;
|
||||
|
||||
|
||||
BinaryOperator find_operator_function_by_string(
|
||||
const size_t number_of_operator_functions,
|
||||
const OperatorFunction * const operator_functions,
|
||||
const char* const operator_string);
|
||||
|
||||
int perform_operation(
|
||||
int number_of_arguments, char *arguments[],
|
||||
const size_t number_of_operator_functions,
|
||||
const OperatorFunction * const operator_functions,
|
||||
int * const number_of_intermediate_values,
|
||||
int ** const intermediate_values, int * const error_occurred);
|
||||
|
||||
static int add(int a, int b);
|
||||
static int subtract(int a, int b);
|
||||
static int multiply(int a, int b);
|
||||
static int divide(int a, int b);
|
||||
|
||||
/* Associate operator strings to functions. */
|
||||
static OperatorFunction operator_function_map[] = {
|
||||
{"+", add},
|
||||
{"-", subtract},
|
||||
{"*", multiply},
|
||||
{"/", divide},
|
||||
};
|
||||
|
||||
static int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
static int subtract(int a, int b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
static int multiply(int a, int b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
static int divide(int a, int b) {
|
||||
assert(b); /* Check for divide by zero. */
|
||||
return a / b;
|
||||
}
|
||||
|
||||
/* Searches the specified array of operator_functions for the function
|
||||
* associated with the specified operator_string. This function returns the
|
||||
* function associated with operator_string if successful, NULL otherwise.
|
||||
*/
|
||||
BinaryOperator find_operator_function_by_string(
|
||||
const size_t number_of_operator_functions,
|
||||
const OperatorFunction * const operator_functions,
|
||||
const char* const operator_string) {
|
||||
size_t i;
|
||||
assert(!number_of_operator_functions || operator_functions);
|
||||
assert(operator_string != NULL);
|
||||
|
||||
for (i = 0; i < number_of_operator_functions; i++) {
|
||||
const OperatorFunction *const operator_function =
|
||||
&operator_functions[i];
|
||||
if (strcmp(operator_function->operator, operator_string) == 0) {
|
||||
return operator_function->function;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Perform a series of binary arithmetic integer operations with no operator
|
||||
* precedence.
|
||||
*
|
||||
* The input expression is specified by arguments which is an array of
|
||||
* containing number_of_arguments strings. Operators invoked by the expression
|
||||
* are specified by the array operator_functions containing
|
||||
* number_of_operator_functions, OperatorFunction structures. The value of
|
||||
* each binary operation is stored in a pointer returned to intermediate_values
|
||||
* which is allocated by malloc().
|
||||
*
|
||||
* If successful, this function returns the integer result of the operations.
|
||||
* If an error occurs while performing the operation error_occurred is set to
|
||||
* 1, the operation is aborted and 0 is returned.
|
||||
*/
|
||||
int perform_operation(
|
||||
int number_of_arguments, char *arguments[],
|
||||
const size_t number_of_operator_functions,
|
||||
const OperatorFunction * const operator_functions,
|
||||
int * const number_of_intermediate_values,
|
||||
int ** const intermediate_values, int * const error_occurred) {
|
||||
char *end_of_integer;
|
||||
int value;
|
||||
int i;
|
||||
assert(!number_of_arguments || arguments);
|
||||
assert(!number_of_operator_functions || operator_functions);
|
||||
assert(error_occurred != NULL);
|
||||
assert(number_of_intermediate_values != NULL);
|
||||
assert(intermediate_values != NULL);
|
||||
|
||||
*error_occurred = 0;
|
||||
*number_of_intermediate_values = 0;
|
||||
*intermediate_values = NULL;
|
||||
if (!number_of_arguments)
|
||||
return 0;
|
||||
|
||||
/* Parse the first value. */
|
||||
value = (int)strtol(arguments[0], &end_of_integer, 10);
|
||||
if (end_of_integer == arguments[0]) {
|
||||
/* If an error occurred while parsing the integer. */
|
||||
fprintf(stderr, "Unable to parse integer from argument %s\n",
|
||||
arguments[0]);
|
||||
*error_occurred = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Allocate an array for the output values. */
|
||||
*intermediate_values = calloc(((number_of_arguments - 1) / 2),
|
||||
sizeof(**intermediate_values));
|
||||
|
||||
i = 1;
|
||||
while (i < number_of_arguments) {
|
||||
int other_value;
|
||||
const char* const operator_string = arguments[i];
|
||||
const BinaryOperator function = find_operator_function_by_string(
|
||||
number_of_operator_functions, operator_functions, operator_string);
|
||||
int * const intermediate_value =
|
||||
&((*intermediate_values)[*number_of_intermediate_values]);
|
||||
(*number_of_intermediate_values) ++;
|
||||
|
||||
if (!function) {
|
||||
fprintf(stderr, "Unknown operator %s, argument %d\n",
|
||||
operator_string, i);
|
||||
*error_occurred = 1;
|
||||
break;
|
||||
}
|
||||
i ++;
|
||||
|
||||
if (i == number_of_arguments) {
|
||||
fprintf(stderr, "Binary operator %s missing argument\n",
|
||||
operator_string);
|
||||
*error_occurred = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
other_value = (int)strtol(arguments[i], &end_of_integer, 10);
|
||||
if (end_of_integer == arguments[i]) {
|
||||
/* If an error occurred while parsing the integer. */
|
||||
fprintf(stderr, "Unable to parse integer %s of argument %d\n",
|
||||
arguments[i], i);
|
||||
*error_occurred = 1;
|
||||
break;
|
||||
}
|
||||
i ++;
|
||||
|
||||
/* Perform the operation and store the intermediate value. */
|
||||
*intermediate_value = function(value, other_value);
|
||||
value = *intermediate_value;
|
||||
}
|
||||
if (*error_occurred) {
|
||||
free(*intermediate_values);
|
||||
*intermediate_values = NULL;
|
||||
*number_of_intermediate_values = 0;
|
||||
return 0;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int return_value;
|
||||
int number_of_intermediate_values;
|
||||
int *intermediate_values;
|
||||
/* Peform the operation. */
|
||||
const int result = perform_operation(
|
||||
argc - 1, &argv[1],
|
||||
sizeof(operator_function_map) / sizeof(operator_function_map[0]),
|
||||
operator_function_map, &number_of_intermediate_values,
|
||||
&intermediate_values, &return_value);
|
||||
|
||||
/* If no errors occurred display the result. */
|
||||
if (!return_value && argc > 1) {
|
||||
int i;
|
||||
int intermediate_value_index = 0;
|
||||
printf("%s\n", argv[1]);
|
||||
for (i = 2; i < argc; i += 2) {
|
||||
assert(intermediate_value_index < number_of_intermediate_values);
|
||||
printf(" %s %s = %d\n", argv[i], argv[i + 1],
|
||||
intermediate_values[intermediate_value_index++]);
|
||||
}
|
||||
printf("= %d\n", result);
|
||||
}
|
||||
if (intermediate_values) {
|
||||
free(intermediate_values);
|
||||
}
|
||||
|
||||
return return_value;
|
||||
}
|
||||
482
tests/cmocka-1.1.0/example/calculator_test.c
Normal file
482
tests/cmocka-1.1.0/example/calculator_test.c
Normal file
@@ -0,0 +1,482 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include "cmocka.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
/* Compatibility with the Windows standard C library. */
|
||||
#define vsnprintf _vsnprintf
|
||||
#endif /* _WIN32 */
|
||||
|
||||
#define array_length(x) (sizeof(x) / sizeof((x)[0]))
|
||||
|
||||
/* To simplify this code, these functions and data structures could have been
|
||||
* separated out from the application example.c into a header shared with
|
||||
* test application. However, this example illustrates how it's possible to
|
||||
* test existing code with little modification. */
|
||||
|
||||
typedef int (*BinaryOperator)(int a, int b);
|
||||
|
||||
typedef struct OperatorFunction {
|
||||
const char* operator;
|
||||
BinaryOperator function;
|
||||
} OperatorFunction;
|
||||
|
||||
extern int add(int a, int b);
|
||||
extern int subtract(int a, int b);
|
||||
extern int multiply(int a, int b);
|
||||
extern int divide(int a, int b);
|
||||
extern BinaryOperator find_operator_function_by_string(
|
||||
const size_t number_of_operator_functions,
|
||||
const OperatorFunction * const operator_functions,
|
||||
const char* const operator_string);
|
||||
extern int perform_operation(
|
||||
int number_of_arguments, char *arguments[],
|
||||
const size_t number_of_operator_functions,
|
||||
const OperatorFunction * const operator_functions,
|
||||
int * const number_of_intermediate_values,
|
||||
int ** const intermediate_values, int * const error_occurred);
|
||||
extern int example_main(int argc, char *argv[]);
|
||||
|
||||
int example_test_fprintf(FILE* const file, const char *format, ...) CMOCKA_PRINTF_ATTRIBUTE(2, 3);
|
||||
int example_test_printf(const char *format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
|
||||
|
||||
static char temporary_buffer[256];
|
||||
|
||||
/* A mock fprintf function that checks the value of strings printed to the
|
||||
* standard error stream. */
|
||||
int example_test_fprintf(FILE* const file, const char *format, ...) {
|
||||
int return_value;
|
||||
va_list args;
|
||||
assert_true(file == stderr);
|
||||
va_start(args, format);
|
||||
return_value = vsnprintf(temporary_buffer, sizeof(temporary_buffer),
|
||||
format, args);
|
||||
check_expected_ptr(temporary_buffer);
|
||||
va_end(args);
|
||||
return return_value;
|
||||
}
|
||||
|
||||
/* A mock printf function that checks the value of strings printed to the
|
||||
* standard output stream. */
|
||||
int example_test_printf(const char *format, ...) {
|
||||
int return_value;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
return_value = vsnprintf(temporary_buffer, sizeof(temporary_buffer),
|
||||
format, args);
|
||||
check_expected_ptr(temporary_buffer);
|
||||
va_end(args);
|
||||
return return_value;
|
||||
}
|
||||
|
||||
/* A mock binary operator function. */
|
||||
static int binary_operator(int a, int b) {
|
||||
check_expected(a);
|
||||
check_expected(b);
|
||||
return (int)mock();
|
||||
}
|
||||
|
||||
|
||||
/* Ensure add() adds two integers correctly. */
|
||||
static void test_add(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_int_equal(add(3, 3), 6);
|
||||
assert_int_equal(add(3, -3), 0);
|
||||
}
|
||||
|
||||
/* Ensure subtract() subtracts two integers correctly. */
|
||||
static void test_subtract(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_int_equal(subtract(3, 3), 0);
|
||||
assert_int_equal(subtract(3, -3), 6);
|
||||
}
|
||||
|
||||
/* Ensure multiple() mulitplies two integers correctly. */
|
||||
static void test_multiply(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_int_equal(multiply(3, 3), 9);
|
||||
assert_int_equal(multiply(3, 0), 0);
|
||||
}
|
||||
|
||||
/* Ensure divide() divides one integer by another correctly. */
|
||||
static void test_divide(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_int_equal(divide(10, 2), 5);
|
||||
assert_int_equal(divide(2, 10), 0);
|
||||
}
|
||||
|
||||
/* Ensure divide() asserts when trying to divide by zero. */
|
||||
static void test_divide_by_zero(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_assert_failure(divide(100, 0));
|
||||
}
|
||||
|
||||
/* Ensure find_operator_function_by_string() asserts when a NULL pointer is
|
||||
* specified as the table to search. */
|
||||
static void test_find_operator_function_by_string_null_functions(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_assert_failure(find_operator_function_by_string(1, NULL, "test"));
|
||||
}
|
||||
|
||||
/* Ensure find_operator_function_by_string() asserts when a NULL pointer is
|
||||
* specified as the string to search for. */
|
||||
static void test_find_operator_function_by_string_null_string(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", binary_operator},
|
||||
};
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_assert_failure(find_operator_function_by_string(
|
||||
array_length(operator_functions), operator_functions, NULL));
|
||||
}
|
||||
|
||||
/* Ensure find_operator_function_by_string() returns NULL when a NULL pointer
|
||||
* is specified as the table to search when the table size is 0. */
|
||||
static void test_find_operator_function_by_string_valid_null_functions(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_null(find_operator_function_by_string(0, NULL, "test"));
|
||||
}
|
||||
|
||||
/* Ensure find_operator_function_by_string() returns NULL when searching for
|
||||
* an operator string that isn't in the specified table. */
|
||||
static void test_find_operator_function_by_string_not_found(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", binary_operator},
|
||||
{"-", binary_operator},
|
||||
{"/", binary_operator},
|
||||
};
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_null(find_operator_function_by_string(
|
||||
array_length(operator_functions), operator_functions, "test"));
|
||||
}
|
||||
|
||||
/* Ensure find_operator_function_by_string() returns the correct function when
|
||||
* searching for an operator string that is in the specified table. */
|
||||
static void test_find_operator_function_by_string_found(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", (BinaryOperator)0x12345678},
|
||||
{"-", (BinaryOperator)0xDEADBEEF},
|
||||
{"/", (BinaryOperator)0xABADCAFE},
|
||||
};
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_int_equal(
|
||||
cast_ptr_to_largest_integral_type(
|
||||
find_operator_function_by_string(array_length(operator_functions),
|
||||
operator_functions,
|
||||
"-")),
|
||||
0xDEADBEEF);
|
||||
}
|
||||
|
||||
/* Ensure perform_operation() asserts when a NULL arguments array is specified. */
|
||||
static void test_perform_operation_null_args(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", binary_operator},
|
||||
};
|
||||
int number_of_intermediate_values;
|
||||
int *intermediate_values;
|
||||
int error_occurred;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_assert_failure(perform_operation(
|
||||
1, NULL, array_length(operator_functions), operator_functions,
|
||||
&number_of_intermediate_values, &intermediate_values,
|
||||
&error_occurred));
|
||||
}
|
||||
|
||||
/* Ensure perform_operation() asserts when a NULL operator_functions array is
|
||||
* specified. */
|
||||
static void test_perform_operation_null_operator_functions(void **state) {
|
||||
const char *args[] = {
|
||||
"1", "+", "2", "*", "4"
|
||||
};
|
||||
int number_of_intermediate_values;
|
||||
int *intermediate_values;
|
||||
int error_occurred;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_assert_failure(perform_operation(
|
||||
array_length(args), (char **) args, 1, NULL, &number_of_intermediate_values,
|
||||
&intermediate_values, &error_occurred));
|
||||
}
|
||||
|
||||
/* Ensure perform_operation() asserts when a NULL pointer is specified for
|
||||
* number_of_intermediate_values. */
|
||||
static void test_perform_operation_null_number_of_intermediate_values(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", binary_operator},
|
||||
};
|
||||
const char *args[] = {
|
||||
"1", "+", "2", "*", "4"
|
||||
};
|
||||
int *intermediate_values;
|
||||
int error_occurred;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_assert_failure(perform_operation(
|
||||
array_length(args), (char **) args, 1, operator_functions, NULL,
|
||||
&intermediate_values, &error_occurred));
|
||||
}
|
||||
|
||||
/* Ensure perform_operation() asserts when a NULL pointer is specified for
|
||||
* intermediate_values. */
|
||||
static void test_perform_operation_null_intermediate_values(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", binary_operator},
|
||||
};
|
||||
const char *args[] = {
|
||||
"1", "+", "2", "*", "4"
|
||||
};
|
||||
int number_of_intermediate_values;
|
||||
int error_occurred;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_assert_failure(perform_operation(
|
||||
array_length(args), (char **) args, array_length(operator_functions),
|
||||
operator_functions, &number_of_intermediate_values, NULL,
|
||||
&error_occurred));
|
||||
}
|
||||
|
||||
/* Ensure perform_operation() returns 0 when no arguments are specified. */
|
||||
static void test_perform_operation_no_arguments(void **state) {
|
||||
int number_of_intermediate_values;
|
||||
int *intermediate_values;
|
||||
int error_occurred;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_int_equal(perform_operation(
|
||||
0, NULL, 0, NULL, &number_of_intermediate_values, &intermediate_values,
|
||||
&error_occurred), 0);
|
||||
assert_int_equal(error_occurred, 0);
|
||||
}
|
||||
|
||||
/* Ensure perform_operation() returns an error if the first argument isn't
|
||||
* an integer string. */
|
||||
static void test_perform_operation_first_arg_not_integer(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", binary_operator},
|
||||
};
|
||||
const char *args[] = {
|
||||
"test", "+", "2", "*", "4"
|
||||
};
|
||||
int number_of_intermediate_values;
|
||||
int *intermediate_values;
|
||||
int error_occurred;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_string(example_test_fprintf, temporary_buffer,
|
||||
"Unable to parse integer from argument test\n");
|
||||
|
||||
assert_int_equal(perform_operation(
|
||||
array_length(args), (char **) args, array_length(operator_functions),
|
||||
operator_functions, &number_of_intermediate_values,
|
||||
&intermediate_values, &error_occurred), 0);
|
||||
assert_int_equal(error_occurred, 1);
|
||||
}
|
||||
|
||||
/* Ensure perform_operation() returns an error when parsing an unknown
|
||||
* operator. */
|
||||
static void test_perform_operation_unknown_operator(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", binary_operator},
|
||||
};
|
||||
const char *args[] = {
|
||||
"1", "*", "2", "*", "4"
|
||||
};
|
||||
int number_of_intermediate_values;
|
||||
int *intermediate_values;
|
||||
int error_occurred;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_string(example_test_fprintf, temporary_buffer,
|
||||
"Unknown operator *, argument 1\n");
|
||||
|
||||
assert_int_equal(perform_operation(
|
||||
array_length(args), (char **) args, array_length(operator_functions),
|
||||
operator_functions, &number_of_intermediate_values,
|
||||
&intermediate_values, &error_occurred), 0);
|
||||
assert_int_equal(error_occurred, 1);
|
||||
}
|
||||
|
||||
/* Ensure perform_operation() returns an error when nothing follows an
|
||||
* operator. */
|
||||
static void test_perform_operation_missing_argument(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", binary_operator},
|
||||
};
|
||||
const char *args[] = {
|
||||
"1", "+",
|
||||
};
|
||||
int number_of_intermediate_values;
|
||||
int *intermediate_values;
|
||||
int error_occurred;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_string(example_test_fprintf, temporary_buffer,
|
||||
"Binary operator + missing argument\n");
|
||||
|
||||
assert_int_equal(perform_operation(
|
||||
array_length(args), (char **) args, array_length(operator_functions),
|
||||
operator_functions, &number_of_intermediate_values,
|
||||
&intermediate_values, &error_occurred), 0);
|
||||
assert_int_equal(error_occurred, 1);
|
||||
}
|
||||
|
||||
/* Ensure perform_operation() returns an error when an integer doesn't follow
|
||||
* an operator. */
|
||||
static void test_perform_operation_no_integer_after_operator(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", binary_operator},
|
||||
};
|
||||
const char *args[] = {
|
||||
"1", "+", "test",
|
||||
};
|
||||
int number_of_intermediate_values;
|
||||
int *intermediate_values;
|
||||
int error_occurred;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_string(example_test_fprintf, temporary_buffer,
|
||||
"Unable to parse integer test of argument 2\n");
|
||||
|
||||
assert_int_equal(perform_operation(
|
||||
array_length(args), (char **) args, array_length(operator_functions),
|
||||
operator_functions, &number_of_intermediate_values,
|
||||
&intermediate_values, &error_occurred), 0);
|
||||
assert_int_equal(error_occurred, 1);
|
||||
}
|
||||
|
||||
|
||||
/* Ensure perform_operation() succeeds given valid input parameters. */
|
||||
static void test_perform_operation(void **state) {
|
||||
const OperatorFunction operator_functions[] = {
|
||||
{"+", binary_operator},
|
||||
{"*", binary_operator},
|
||||
};
|
||||
const char *args[] = {
|
||||
"1", "+", "3", "*", "10",
|
||||
};
|
||||
int number_of_intermediate_values;
|
||||
int *intermediate_values = NULL;
|
||||
int error_occurred;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
/* Setup return values of mock operator functions. */
|
||||
/* Addition. */
|
||||
expect_value(binary_operator, a, 1);
|
||||
expect_value(binary_operator, b, 3);
|
||||
will_return(binary_operator, 4);
|
||||
|
||||
/* Multiplication. */
|
||||
expect_value(binary_operator, a, 4);
|
||||
expect_value(binary_operator, b, 10);
|
||||
will_return(binary_operator, 40);
|
||||
|
||||
assert_int_equal(perform_operation(
|
||||
array_length(args), (char **) args, array_length(operator_functions),
|
||||
operator_functions, &number_of_intermediate_values,
|
||||
&intermediate_values, &error_occurred), 40);
|
||||
assert_int_equal(error_occurred, 0);
|
||||
|
||||
assert_non_null(intermediate_values);
|
||||
assert_int_equal(intermediate_values[0], 4);
|
||||
assert_int_equal(intermediate_values[1], 40);
|
||||
test_free(intermediate_values);
|
||||
}
|
||||
|
||||
|
||||
/* Ensure main() in example.c succeeds given no arguments. */
|
||||
static void test_example_main_no_args(void **state) {
|
||||
const char *args[] = {
|
||||
"example",
|
||||
};
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_int_equal(example_main(array_length(args), (char **) args), 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Ensure main() in example.c succeeds given valid input arguments. */
|
||||
static void test_example_main(void **state) {
|
||||
const char *args[] = {
|
||||
"example", "1", "+", "3", "*", "10",
|
||||
};
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_string(example_test_printf, temporary_buffer, "1\n");
|
||||
expect_string(example_test_printf, temporary_buffer, " + 3 = 4\n");
|
||||
expect_string(example_test_printf, temporary_buffer, " * 10 = 40\n");
|
||||
expect_string(example_test_printf, temporary_buffer, "= 40\n");
|
||||
|
||||
assert_int_equal(example_main(array_length(args), (char **) args), 0);
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_add),
|
||||
cmocka_unit_test(test_subtract),
|
||||
cmocka_unit_test(test_multiply),
|
||||
cmocka_unit_test(test_divide),
|
||||
cmocka_unit_test(test_divide_by_zero),
|
||||
cmocka_unit_test(test_find_operator_function_by_string_null_functions),
|
||||
cmocka_unit_test(test_find_operator_function_by_string_null_string),
|
||||
cmocka_unit_test(test_find_operator_function_by_string_valid_null_functions),
|
||||
cmocka_unit_test(test_find_operator_function_by_string_not_found),
|
||||
cmocka_unit_test(test_find_operator_function_by_string_found),
|
||||
cmocka_unit_test(test_perform_operation_null_args),
|
||||
cmocka_unit_test(test_perform_operation_null_operator_functions),
|
||||
cmocka_unit_test(test_perform_operation_null_number_of_intermediate_values),
|
||||
cmocka_unit_test(test_perform_operation_null_intermediate_values),
|
||||
cmocka_unit_test(test_perform_operation_no_arguments),
|
||||
cmocka_unit_test(test_perform_operation_first_arg_not_integer),
|
||||
cmocka_unit_test(test_perform_operation_unknown_operator),
|
||||
cmocka_unit_test(test_perform_operation_missing_argument),
|
||||
cmocka_unit_test(test_perform_operation_no_integer_after_operator),
|
||||
cmocka_unit_test(test_perform_operation),
|
||||
cmocka_unit_test(test_example_main_no_args),
|
||||
cmocka_unit_test(test_example_main),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
20
tests/cmocka-1.1.0/example/chef_wrap/CMakeLists.txt
Normal file
20
tests/cmocka-1.1.0/example/chef_wrap/CMakeLists.txt
Normal file
@@ -0,0 +1,20 @@
|
||||
project(cmocka-wrap-examples C)
|
||||
|
||||
include_directories(
|
||||
${CMAKE_BINARY_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMOCKA_PUBLIC_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
add_executable(waiter_test_wrap waiter_test_wrap.c chef.c)
|
||||
target_link_libraries(waiter_test_wrap ${CMOCKA_SHARED_LIBRARY})
|
||||
|
||||
add_test(waiter_test_wrap ${CMAKE_CURRENT_BINARY_DIR}/waiter_test_wrap)
|
||||
|
||||
set_target_properties(waiter_test_wrap
|
||||
PROPERTIES
|
||||
LINK_FLAGS "-Wl,--wrap=chef_cook"
|
||||
)
|
||||
if (WIN32 OR MINGW OR CYGWIN)
|
||||
set_tests_properties(waiter_test_wrap PROPERTIES ENVIRONMENT "PATH=${DLL_PATH_ENV}")
|
||||
endif (WIN32 OR MINGW OR CYGWIN)
|
||||
54
tests/cmocka-1.1.0/example/chef_wrap/chef.c
Normal file
54
tests/cmocka-1.1.0/example/chef_wrap/chef.c
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2013 (c) Andreas Schneider <asn@cynapses.org>
|
||||
* Jakub Hrozek <jakub.hrozek@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "chef.h"
|
||||
|
||||
|
||||
/* This is the real chef, just not implemented yet, currently it always
|
||||
* returns ENOSYS
|
||||
*/
|
||||
int chef_cook(const char *order, char **dish_out)
|
||||
{
|
||||
if (order == NULL || dish_out == NULL) return EINVAL;
|
||||
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
/* Print chef return codes as string */
|
||||
const char *chef_strerror(int error)
|
||||
{
|
||||
switch (error) {
|
||||
case 0:
|
||||
return "Success";
|
||||
case -1:
|
||||
return "Unknown dish";
|
||||
case -2:
|
||||
return "Not enough ingredients for the dish";
|
||||
}
|
||||
|
||||
return "Unknown error!";
|
||||
}
|
||||
|
||||
19
tests/cmocka-1.1.0/example/chef_wrap/chef.h
Normal file
19
tests/cmocka-1.1.0/example/chef_wrap/chef.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright 2013 (c) Andreas Schneider <asn@cynapses.org>
|
||||
* Jakub Hrozek <jakub.hrozek@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
int chef_cook(const char *order, char **dish_out);
|
||||
const char *chef_strerror(int error);
|
||||
176
tests/cmocka-1.1.0/example/chef_wrap/waiter_test_wrap.c
Normal file
176
tests/cmocka-1.1.0/example/chef_wrap/waiter_test_wrap.c
Normal file
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* Copyright 2013 (c) Andreas Schneider <asn@cynapses.org>
|
||||
* Jakub Hrozek <jakub.hrozek@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "waiter_test_wrap.h"
|
||||
#include "chef.h"
|
||||
|
||||
/*
|
||||
* This is a mocked Chef object. A real Chef would look if he knows
|
||||
* the dish in some kind of internal database and check his storage for
|
||||
* ingredients. This chef simply retrieves this information from the test
|
||||
* that is calling him.
|
||||
*
|
||||
* This object is also wrapped - if any code links with this file and is
|
||||
* compiled with linker option --wrap chef_cook, any calls of that code to
|
||||
* chef_cook will end up calling __wrap_chef_cook.
|
||||
*
|
||||
* If for any reason the wrapped function wanted to call the real chef_cook()
|
||||
* function, it could do so by calling the special symbol __real_chef_cook().
|
||||
*
|
||||
* Please note that when setting return codes for the chef_cook function, we
|
||||
* use this wrapper as a parameter for the will_return() macro, not the
|
||||
* real function.
|
||||
*
|
||||
* A chef object would return:
|
||||
* 0 - cooking dish went fine
|
||||
* -1 - unknown dish
|
||||
* -2 - ran out of ingredients for the dish
|
||||
* any other error code -- unexpected error while cooking
|
||||
*
|
||||
* The return codes should be consistent between the real and mocked objects.
|
||||
*/
|
||||
int __wrap_chef_cook(const char *order, char **dish_out)
|
||||
{
|
||||
bool has_ingredients;
|
||||
bool knows_dish;
|
||||
char *dish;
|
||||
|
||||
check_expected_ptr(order);
|
||||
|
||||
knows_dish = mock_type(bool);
|
||||
if (knows_dish == false) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
has_ingredients = mock_type(bool);
|
||||
if (has_ingredients == false) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
dish = mock_ptr_type(char *);
|
||||
*dish_out = strdup(dish);
|
||||
if (*dish_out == NULL) return ENOMEM;
|
||||
|
||||
return mock_type(int);
|
||||
}
|
||||
|
||||
/* Waiter return codes:
|
||||
* 0 - success
|
||||
* -1 - kitchen failed
|
||||
* -2 - kitchen succeeded, but cooked a different food
|
||||
*/
|
||||
static int waiter_process(const char *order, char **dish)
|
||||
{
|
||||
int rv;
|
||||
|
||||
rv = chef_cook(order, dish);
|
||||
if (rv != 0) {
|
||||
fprintf(stderr, "Chef couldn't cook %s: %s\n",
|
||||
order, chef_strerror(rv));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check if we received the dish we wanted from the kitchen */
|
||||
if (strcmp(order, *dish) != 0) {
|
||||
free(*dish);
|
||||
*dish = NULL;
|
||||
return -2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_order_hotdog(void **state)
|
||||
{
|
||||
int rv;
|
||||
char *dish;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
/* We expect the chef to receive an order for a hotdog */
|
||||
expect_string(__wrap_chef_cook, order, "hotdog");
|
||||
/* And we tell the test chef that ke knows how to cook a hotdog
|
||||
* and has the ingredients
|
||||
*/
|
||||
will_return(__wrap_chef_cook, true);
|
||||
will_return(__wrap_chef_cook, true);
|
||||
/* The result will be a hotdog and the cooking process will succeed */
|
||||
will_return(__wrap_chef_cook, cast_ptr_to_largest_integral_type("hotdog"));
|
||||
will_return(__wrap_chef_cook, 0);
|
||||
|
||||
/* Test the waiter */
|
||||
rv = waiter_process("hotdog", &dish);
|
||||
|
||||
/* We expect the cook to succeed cooking the hotdog */
|
||||
assert_int_equal(rv, 0);
|
||||
/* And actually receive one */
|
||||
assert_string_equal(dish, "hotdog");
|
||||
if (dish != NULL) {
|
||||
free(dish);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_bad_dish(void **state)
|
||||
{
|
||||
int rv;
|
||||
char *dish;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
/* We expect the chef to receive an order for a hotdog */
|
||||
expect_string(__wrap_chef_cook, order, "hotdog");
|
||||
/* And we tell the test chef that ke knows how to cook a hotdog
|
||||
* and has the ingredients
|
||||
*/
|
||||
will_return(__wrap_chef_cook, true);
|
||||
will_return(__wrap_chef_cook, true);
|
||||
/* The result will be a burger and the cooking process will succeed.
|
||||
* We expect the waiter to handle the bad dish and return an error
|
||||
* code
|
||||
*/
|
||||
will_return(__wrap_chef_cook, cast_ptr_to_largest_integral_type("burger"));
|
||||
will_return(__wrap_chef_cook, 0);
|
||||
|
||||
/* Test the waiter */
|
||||
rv = waiter_process("hotdog", &dish);
|
||||
|
||||
/* According to the documentation the waiter should return -2 now */
|
||||
assert_int_equal(rv, -2);
|
||||
/* And do not give the bad dish to the customer */
|
||||
assert_null(dish);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_order_hotdog),
|
||||
cmocka_unit_test(test_bad_dish),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
2
tests/cmocka-1.1.0/example/chef_wrap/waiter_test_wrap.h
Normal file
2
tests/cmocka-1.1.0/example/chef_wrap/waiter_test_wrap.h
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
int __wrap_chef_cook(const char *order, char **dish_out);
|
||||
51
tests/cmocka-1.1.0/example/customer_database.c
Normal file
51
tests/cmocka-1.1.0/example/customer_database.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <database.h>
|
||||
#ifdef _WIN32
|
||||
#define snprintf _snprintf
|
||||
#endif /* _WIN32 */
|
||||
|
||||
DatabaseConnection* connect_to_customer_database(void);
|
||||
unsigned int get_customer_id_by_name(
|
||||
DatabaseConnection * const connection,
|
||||
const char * const customer_name);
|
||||
|
||||
/* Connect to the database containing customer information. */
|
||||
DatabaseConnection* connect_to_customer_database(void) {
|
||||
return connect_to_database("customers.abcd.org", 321);
|
||||
}
|
||||
|
||||
/* Find the ID of a customer by his/her name returning a value > 0 if
|
||||
* successful, 0 otherwise. */
|
||||
unsigned int get_customer_id_by_name(
|
||||
DatabaseConnection * const connection,
|
||||
const char * const customer_name) {
|
||||
char query_string[256];
|
||||
int number_of_results;
|
||||
void **results;
|
||||
snprintf(query_string, sizeof(query_string),
|
||||
"SELECT ID FROM CUSTOMERS WHERE NAME = %s", customer_name);
|
||||
number_of_results = connection->query_database(connection, query_string,
|
||||
&results);
|
||||
|
||||
if (number_of_results != 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (unsigned int)*((int *)results);
|
||||
}
|
||||
89
tests/cmocka-1.1.0/example/customer_database_test.c
Normal file
89
tests/cmocka-1.1.0/example/customer_database_test.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
#include <database.h>
|
||||
|
||||
extern DatabaseConnection* connect_to_customer_database();
|
||||
extern unsigned int get_customer_id_by_name(
|
||||
DatabaseConnection * const connection, const char * const customer_name);
|
||||
|
||||
/* Mock query database function. */
|
||||
static unsigned int mock_query_database(DatabaseConnection* const connection,
|
||||
const char * const query_string,
|
||||
void *** const results) {
|
||||
(void) connection; /* unused */
|
||||
(void) query_string; /* unused */
|
||||
|
||||
*results = (void **)mock_ptr_type(int *);
|
||||
return mock_ptr_type(int);
|
||||
}
|
||||
|
||||
/* Mock of the connect to database function. */
|
||||
DatabaseConnection* connect_to_database(const char * const database_url,
|
||||
const unsigned int port) {
|
||||
(void) database_url; /* unused */
|
||||
(void) port; /* unused */
|
||||
|
||||
return (DatabaseConnection*)((size_t)mock());
|
||||
}
|
||||
|
||||
static void test_connect_to_customer_database(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
will_return(connect_to_database, 0x0DA7ABA53);
|
||||
|
||||
assert_int_equal((size_t)connect_to_customer_database(), 0x0DA7ABA53);
|
||||
}
|
||||
|
||||
/* This test fails as the mock function connect_to_database() will have no
|
||||
* value to return. */
|
||||
#if 0
|
||||
static void fail_connect_to_customer_database(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
assert_true(connect_to_customer_database() ==
|
||||
(DatabaseConnection*)0x0DA7ABA53);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void test_get_customer_id_by_name(void **state) {
|
||||
DatabaseConnection connection = {
|
||||
"somedatabase.somewhere.com", 12345678, mock_query_database
|
||||
};
|
||||
/* Return a single customer ID when mock_query_database() is called. */
|
||||
int customer_ids = 543;
|
||||
int rc;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
will_return(mock_query_database,
|
||||
cast_ptr_to_largest_integral_type(&customer_ids));
|
||||
will_return(mock_query_database, 1);
|
||||
|
||||
rc = get_customer_id_by_name(&connection, "john doe");
|
||||
assert_int_equal(rc, 543);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_connect_to_customer_database),
|
||||
cmocka_unit_test(test_get_customer_id_by_name),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
37
tests/cmocka-1.1.0/example/database.h
Normal file
37
tests/cmocka-1.1.0/example/database.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
typedef struct DatabaseConnection DatabaseConnection;
|
||||
|
||||
/* Function that takes an SQL query string and sets results to an array of
|
||||
* pointers with the result of the query. The value returned specifies the
|
||||
* number of items in the returned array of results. The returned array of
|
||||
* results are statically allocated and should not be deallocated using free()
|
||||
*/
|
||||
typedef unsigned int (*QueryDatabase)(
|
||||
DatabaseConnection* const connection, const char * const query_string,
|
||||
void *** const results);
|
||||
|
||||
/* Connection to a database. */
|
||||
struct DatabaseConnection {
|
||||
const char *url;
|
||||
unsigned int port;
|
||||
QueryDatabase query_database;
|
||||
};
|
||||
|
||||
/* Connect to a database. */
|
||||
DatabaseConnection* connect_to_database(const char * const url,
|
||||
const unsigned int port);
|
||||
|
||||
51
tests/cmocka-1.1.0/example/key_value.c
Normal file
51
tests/cmocka-1.1.0/example/key_value.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "key_value.h"
|
||||
|
||||
static KeyValue *key_values = NULL;
|
||||
static unsigned int number_of_key_values = 0;
|
||||
|
||||
void set_key_values(KeyValue * const new_key_values,
|
||||
const unsigned int new_number_of_key_values) {
|
||||
key_values = new_key_values;
|
||||
number_of_key_values = new_number_of_key_values;
|
||||
}
|
||||
|
||||
/* Compare two key members of KeyValue structures. */
|
||||
static int key_value_compare_keys(const void *a, const void *b) {
|
||||
return (int)((KeyValue*)a)->key - (int)((KeyValue*)b)->key;
|
||||
}
|
||||
|
||||
/* Search an array of key value pairs for the item with the specified value. */
|
||||
KeyValue* find_item_by_value(const char * const value) {
|
||||
unsigned int i;
|
||||
for (i = 0; i < number_of_key_values; i++) {
|
||||
if (strcmp(key_values[i].value, value) == 0) {
|
||||
return &key_values[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Sort an array of key value pairs by key. */
|
||||
void sort_items_by_key(void) {
|
||||
qsort(key_values, number_of_key_values, sizeof(*key_values),
|
||||
key_value_compare_keys);
|
||||
}
|
||||
27
tests/cmocka-1.1.0/example/key_value.h
Normal file
27
tests/cmocka-1.1.0/example/key_value.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
typedef struct KeyValue {
|
||||
unsigned int key;
|
||||
const char* value;
|
||||
} KeyValue;
|
||||
|
||||
void set_key_values(KeyValue * const new_key_values,
|
||||
const unsigned int new_number_of_key_values);
|
||||
|
||||
KeyValue* find_item_by_value(const char * const value);
|
||||
|
||||
void sort_items_by_key(void);
|
||||
77
tests/cmocka-1.1.0/example/key_value_test.c
Normal file
77
tests/cmocka-1.1.0/example/key_value_test.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <string.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "key_value.h"
|
||||
|
||||
static KeyValue key_values[] = {
|
||||
{ 10, "this" },
|
||||
{ 52, "test" },
|
||||
{ 20, "a" },
|
||||
{ 13, "is" },
|
||||
};
|
||||
|
||||
static int create_key_values(void **state) {
|
||||
KeyValue * const items = (KeyValue*)test_malloc(sizeof(key_values));
|
||||
memcpy(items, key_values, sizeof(key_values));
|
||||
*state = (void*)items;
|
||||
set_key_values(items, sizeof(key_values) / sizeof(key_values[0]));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int destroy_key_values(void **state) {
|
||||
test_free(*state);
|
||||
set_key_values(NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_find_item_by_value(void **state) {
|
||||
unsigned int i;
|
||||
|
||||
(void) state; /* unused */
|
||||
|
||||
for (i = 0; i < sizeof(key_values) / sizeof(key_values[0]); i++) {
|
||||
KeyValue * const found = find_item_by_value(key_values[i].value);
|
||||
assert_true(found != NULL);
|
||||
assert_int_equal(found->key, key_values[i].key);
|
||||
assert_string_equal(found->value, key_values[i].value);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_sort_items_by_key(void **state) {
|
||||
unsigned int i;
|
||||
KeyValue * const kv = *state;
|
||||
sort_items_by_key();
|
||||
for (i = 1; i < sizeof(key_values) / sizeof(key_values[0]); i++) {
|
||||
assert_true(kv[i - 1].key < kv[i].key);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test_setup_teardown(test_find_item_by_value,
|
||||
create_key_values, destroy_key_values),
|
||||
cmocka_unit_test_setup_teardown(test_sort_items_by_key,
|
||||
create_key_values, destroy_key_values),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
24
tests/cmocka-1.1.0/example/product_database.c
Normal file
24
tests/cmocka-1.1.0/example/product_database.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <database.h>
|
||||
|
||||
DatabaseConnection* connect_to_product_database(void);
|
||||
|
||||
/* Connect to the database containing customer information. */
|
||||
DatabaseConnection* connect_to_product_database(void) {
|
||||
return connect_to_database("products.abcd.org", 322);
|
||||
}
|
||||
|
||||
72
tests/cmocka-1.1.0/example/product_database_test.c
Normal file
72
tests/cmocka-1.1.0/example/product_database_test.c
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
#include <database.h>
|
||||
|
||||
extern DatabaseConnection* connect_to_product_database(void);
|
||||
|
||||
/* Mock connect to database function.
|
||||
* NOTE: This mock function is very general could be shared between tests
|
||||
* that use the imaginary database.h module. */
|
||||
DatabaseConnection* connect_to_database(const char * const url,
|
||||
const unsigned int port) {
|
||||
check_expected_ptr(url);
|
||||
check_expected(port);
|
||||
return (DatabaseConnection*)((size_t)mock());
|
||||
}
|
||||
|
||||
static void test_connect_to_product_database(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_string(connect_to_database, url, "products.abcd.org");
|
||||
expect_value(connect_to_database, port, 322);
|
||||
will_return(connect_to_database, 0xDA7ABA53);
|
||||
assert_int_equal((size_t)connect_to_product_database(), 0xDA7ABA53);
|
||||
}
|
||||
|
||||
/* This test will fail since the expected URL is different to the URL that is
|
||||
* passed to connect_to_database() by connect_to_product_database(). */
|
||||
static void test_connect_to_product_database_bad_url(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_string(connect_to_database, url, "products.abcd.com");
|
||||
expect_value(connect_to_database, port, 322);
|
||||
will_return(connect_to_database, 0xDA7ABA53);
|
||||
assert_int_equal((size_t)connect_to_product_database(), 0xDA7ABA53);
|
||||
}
|
||||
|
||||
/* This test will fail since the mock connect_to_database() will attempt to
|
||||
* retrieve a value for the parameter port which isn't specified by this
|
||||
* test function. */
|
||||
static void test_connect_to_product_database_missing_parameter(void **state) {
|
||||
(void) state; /* unused */
|
||||
|
||||
expect_string(connect_to_database, url, "products.abcd.org");
|
||||
will_return(connect_to_database, 0xDA7ABA53);
|
||||
assert_int_equal((size_t)connect_to_product_database(), 0xDA7ABA53);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_connect_to_product_database),
|
||||
cmocka_unit_test(test_connect_to_product_database_bad_url),
|
||||
cmocka_unit_test(test_connect_to_product_database_missing_parameter),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
17
tests/cmocka-1.1.0/example/simple_test.c
Normal file
17
tests/cmocka-1.1.0/example/simple_test.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
/* A test case that does nothing and succeeds. */
|
||||
static void null_test_success(void **state) {
|
||||
(void) state; /* unused */
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(null_test_success),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user