-
Notifications
You must be signed in to change notification settings - Fork 13
[MrBot] Add test cases (parameterized tests) #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
70d6a8f
MrBot first draft at ading test cases
mattdurak 914eecf
Address PR review: simplify parameterized test macros
mattdurak 55e639f
Restore ARGS() syntax via token pasting like CASE
mattdurak 408e43d
Extract parameterized tests to new parameterized_tests_ut project
mattdurak 20c28b8
Remove unnecessary // no-srs and AAA comments from fixture functions
mattdurak 1d79e8a
Simplify PARAMETERIZED_TEST_FUNCTION to passthrough to ctest, add cpp…
mattdurak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #Copyright (c) Microsoft. All rights reserved. | ||
| #Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| set(theseTestsName parameterized_tests_ut) | ||
|
|
||
| set(${theseTestsName}_test_files | ||
| ${theseTestsName}.c | ||
| ) | ||
|
|
||
| set(${theseTestsName}_c_files | ||
| ) | ||
|
|
||
| set(${theseTestsName}_h_files | ||
| ) | ||
|
|
||
| build_test_artifacts(${theseTestsName} "tests/c_testrunnerswitcher") |
127 changes: 127 additions & 0 deletions
127
test_projects/parameterized_tests_ut/parameterized_tests_ut.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| #ifdef CPPUNITTEST_SYMBOL | ||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
| void CPPUNITTEST_SYMBOL(void) {} | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
| #endif /*CPPUNITTEST_SYMBOL*/ | ||
|
|
||
| #include "testrunnerswitcher.h" | ||
|
|
||
| static int add_numbers(int a, int b) | ||
| { | ||
| return a + b; | ||
| } | ||
|
|
||
| static int multiply_numbers(int a, int b) | ||
| { | ||
| return a * b; | ||
| } | ||
|
|
||
| /* Global state to verify parameterized test cases are actually executed with correct parameters */ | ||
| static int g_call_count; | ||
| static int g_accumulated_sum; | ||
|
|
||
| BEGIN_TEST_SUITE(parameterized_tests_ut) | ||
|
|
||
| TEST_SUITE_INITIALIZE(suite_init) | ||
| { | ||
| } | ||
|
|
||
| TEST_SUITE_CLEANUP(suite_cleanup) | ||
| { | ||
| /* assert - verify that the parameterized test cases for test_verify_calls were all executed */ | ||
| /* 4 cases: (1,10), (2,20), (3,30), (4,40) => call_count=4, accumulated_sum = 11+22+33+44 = 110 */ | ||
| ASSERT_ARE_EQUAL(int, 4, g_call_count, "Expected 4 parameterized test cases to execute, but got %d", g_call_count); | ||
| ASSERT_ARE_EQUAL(int, 110, g_accumulated_sum, "Expected accumulated sum of 110, but got %d", g_accumulated_sum); | ||
| } | ||
|
|
||
| TEST_FUNCTION_INITIALIZE(test_init) | ||
| { | ||
| } | ||
|
|
||
| TEST_FUNCTION_CLEANUP(test_cleanup) | ||
| { | ||
| } | ||
|
|
||
| /* | ||
| * Verification test: records global state to prove each CASE is executed with correct parameters. | ||
| * TEST_SUITE_CLEANUP validates that all 4 cases ran and produced the expected accumulated sum. | ||
| */ | ||
| PARAMETERIZED_TEST_FUNCTION(test_verify_calls, // no-srs | ||
| ARGS(int, a, int, b), | ||
| CASE((1, 10), case_1_10), | ||
| CASE((2, 20), case_2_20), | ||
| CASE((3, 30), case_3_30), | ||
| CASE((4, 40), case_4_40)) | ||
| { | ||
| // arrange | ||
| // (parameters a and b are set by the CASE macro) | ||
|
|
||
| // act | ||
| g_call_count++; | ||
| g_accumulated_sum += a + b; | ||
|
|
||
| // assert | ||
| ASSERT_IS_TRUE(a >= 1, "Expected a >= 1, got %d", a); | ||
| ASSERT_IS_TRUE(b >= 10, "Expected b >= 10, got %d", b); | ||
| } | ||
|
|
||
| PARAMETERIZED_TEST_FUNCTION(test_addition, // no-srs | ||
| ARGS(int, a, int, b, int, expected), | ||
| CASE((0, 0, 0), with_zeros), | ||
| CASE((1, 2, 3), with_small_positive_numbers), | ||
| CASE((-1, 1, 0), with_negative_and_positive), | ||
| CASE((100, 200, 300), with_larger_numbers)) | ||
| { | ||
| // arrange | ||
| // (parameters a, b, expected are set by the CASE macro) | ||
|
|
||
| // act | ||
| int result = add_numbers(a, b); | ||
|
|
||
| // assert | ||
| ASSERT_ARE_EQUAL(int, expected, result, "Expected %d + %d = %d, but got %d", a, b, expected, result); | ||
| } | ||
|
|
||
| PARAMETERIZED_TEST_FUNCTION(test_multiplication, // no-srs | ||
| ARGS(int, x, int, y, int, expected_product), | ||
| CASE((0, 5, 0), with_zero), | ||
| CASE((1, 1, 1), with_ones), | ||
| CASE((2, 3, 6), with_small_numbers), | ||
| CASE((-2, 3, -6), with_negative_factor)) | ||
| { | ||
| // arrange | ||
| // (parameters x, y, expected_product are set by the CASE macro) | ||
|
|
||
| // act | ||
| int result = multiply_numbers(x, y); | ||
|
|
||
| // assert | ||
| ASSERT_ARE_EQUAL(int, expected_product, result); | ||
| } | ||
|
|
||
| PARAMETERIZED_TEST_FUNCTION(test_is_positive, // no-srs | ||
| ARGS(int, value, int, expected_is_positive), | ||
| CASE((1, 1), for_positive_one), | ||
| CASE((100, 1), for_hundred), | ||
| CASE((0, 0), for_zero), | ||
| CASE((-1, 0), for_negative_one)) | ||
| { | ||
| // arrange | ||
| // (parameters value, expected_is_positive are set by the CASE macro) | ||
|
|
||
| // act | ||
| int result = (value > 0) ? 1 : 0; | ||
|
|
||
| // assert | ||
| ASSERT_ARE_EQUAL(int, expected_is_positive, result); | ||
| } | ||
|
|
||
| END_TEST_SUITE(parameterized_tests_ut) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.