/* Ensure find_operator_function_by_string() asserts when a NULL pointer is * specified as the string to search for. */ void test_find_operator_function_by_string_null_string(void **state) { const OperatorFunction operator_functions[] = { {"+", binary_operator}, }; expect_assert_failure(find_operator_function_by_string( array_length(operator_functions), operator_functions, NULL)); }
/* Ensure find_operator_function_by_string() returns the correct function when * searching for an operator string that is in the specified table. */ void test_find_operator_function_by_string_found(void **state) { const OperatorFunction operator_functions[] = { {"+", (BinaryOperator)0x12345678}, {"-", (BinaryOperator)0xDEADBEEF}, {"/", (BinaryOperator)0xABADCAFE}, }; assert_int_equal(find_operator_function_by_string( array_length(operator_functions), operator_functions, "-"), 0xDEADBEEF); }
/* Ensure find_operator_function_by_string() returns NULL when searching for * an operator string that isn't in the specified table. */ void test_find_operator_function_by_string_not_found(void **state) { const OperatorFunction operator_functions[] = { {"+", binary_operator}, {"-", binary_operator}, {"/", binary_operator}, }; assert_int_equal(find_operator_function_by_string( array_length(operator_functions), operator_functions, "test"), NULL); }
/* 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 find_operator_function_by_string() returns NULL when a NULL pointer * is specified as the table to search when the table size is 0. */ void test_find_operator_function_by_string_valid_null_functions(void **state) { assert_int_equal(find_operator_function_by_string(0, NULL, "test"), NULL); }
/* Ensure find_operator_function_by_string() asserts when a NULL pointer is * specified as the table to search. */ void test_find_operator_function_by_string_null_functions(void **state) { expect_assert_failure(find_operator_function_by_string(1, NULL, "test")); }
/* 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. */ static 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; unsigned int i; assert(!number_of_arguments || arguments); assert(!number_of_operator_functions || operator_functions); assert(error_occurred); assert(number_of_intermediate_values); assert(intermediate_values); *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; }
/* 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")); }