aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2018-09-27 08:31:42 +0200
committerAndreas Schneider <asn@cryptomilk.org>2018-09-27 14:34:40 +0200
commitbd8ae2b6e044fc3f9410a8e9b851487170f1633e (patch)
tree6004fa5f5202fd260bf4c9ee4ba380da789c03fe /example
parent83e2ece07b8c28686e46b2eb8ed184626242980b (diff)
downloadcmocka-bd8ae2b6e044fc3f9410a8e9b851487170f1633e.tar.gz
cmocka-bd8ae2b6e044fc3f9410a8e9b851487170f1633e.tar.xz
cmocka-bd8ae2b6e044fc3f9410a8e9b851487170f1633e.zip
exmaples: Remove examples which have issues on some platforms
We should provide better examples.
Diffstat (limited to 'example')
-rw-r--r--example/CMakeLists.txt55
-rw-r--r--example/customer_database.c51
-rw-r--r--example/customer_database_test.c89
-rw-r--r--example/key_value.c51
-rw-r--r--example/key_value.h27
-rw-r--r--example/key_value_test.c77
-rw-r--r--example/product_database.c24
-rw-r--r--example/product_database_test.c72
8 files changed, 12 insertions, 434 deletions
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
index f0890d2..46e626a 100644
--- a/example/CMakeLists.txt
+++ b/example/CMakeLists.txt
@@ -1,11 +1,11 @@
project(cmocka-examples C)
-set_source_files_properties(
- calculator.c
- allocate_module.c
- assert_module.c
- PROPERTIES
- COMPILE_DEFINITIONS UNIT_TESTING=1)
+set_source_files_properties(calculator.c
+ allocate_module.c
+ assert_module.c
+ PROPERTIES
+ COMPILE_DEFINITIONS
+ UNIT_TESTING=1)
if (WIN32 OR CYGWIN OR MINGW)
@@ -94,41 +94,10 @@ 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 ${TARGET_SYSTEM_EMULATOR} ${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 ${TARGET_SYSTEM_EMULATOR} ${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 ${TARGET_SYSTEM_EMULATOR} ${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)
- add_subdirectory(uptime)
+if (NOT WIN32)
+ # TODO Execute "$CMAKE_LINKER --help" and check for --wrap
+ if (${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)" AND NOT APPLE)
+ add_subdirectory(chef_wrap)
+ add_subdirectory(uptime)
+ endif()
endif()
diff --git a/example/customer_database.c b/example/customer_database.c
deleted file mode 100644
index 2d49e19..0000000
--- a/example/customer_database.c
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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);
-}
diff --git a/example/customer_database_test.c b/example/customer_database_test.c
deleted file mode 100644
index 45ec782..0000000
--- a/example/customer_database_test.c
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * 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(void);
-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);
-}
diff --git a/example/key_value.c b/example/key_value.c
deleted file mode 100644
index 057274a..0000000
--- a/example/key_value.c
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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);
-}
diff --git a/example/key_value.h b/example/key_value.h
deleted file mode 100644
index 736faf2..0000000
--- a/example/key_value.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * 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);
diff --git a/example/key_value_test.c b/example/key_value_test.c
deleted file mode 100644
index 102a2be..0000000
--- a/example/key_value_test.c
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * 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);
-}
diff --git a/example/product_database.c b/example/product_database.c
deleted file mode 100644
index 980b7e5..0000000
--- a/example/product_database.c
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * 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);
-}
-
diff --git a/example/product_database_test.c b/example/product_database_test.c
deleted file mode 100644
index e09eeab..0000000
--- a/example/product_database_test.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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);
-}