11#define TEC_IMPLEMENTATION
2- #include " ./include/math_utils.h"
3- #include " ./tec.h"
4-
5- #ifdef __cplusplus
2+ #include " tec.h"
63#include < iostream>
74#include < memory>
85#include < vector>
9- TEC (cpp_Style, Vector) {
6+
7+ TEC (cpp_features, Vector) {
108 std::vector<int > my_vector (100 ); // THIS WILL AUTOFREE CAUSE OF RAII(dest)
119
1210 // Dynamically allocated int (manual memory management).
@@ -30,131 +28,53 @@ TEC(cpp_Style, Vector) {
3028 TEC_ASSERT (1 == 1 );
3129}
3230
33- TEC (cpp_Style , SmartPointer) {
31+ TEC (cpp_features , SmartPointer) {
3432 std::unique_ptr<int []> my_array (new int [100 ]);
3533 TEC_ASSERT (my_array != nullptr );
3634}
37- #else
38- TEC_XFAIL (framework, teardown_with_cleanup) {
39- TEC_SKIP (" Fails intentionally to demonstrate try/teardown." );
40- int *value = NULL ;
41-
42- // TRY BLOCK: Contains setup and assertions.
43- // If an assert fails, execution jumps past this block.
44- TEC_TRY_BLOCK {
45- value = malloc (sizeof (int ));
46- TEC_ASSERT_NE (NULL , value);
47- *value = 69 ;
48- TEC_ASSERT_EQ (*value, 420 );
49- }
50-
51- // FINALLY BLOCK: This code is guaranteed to run.
52- // No keyword is needed; it's simply the code that follows the try block.
53- free (value);
54- // printf("Teardown complete: memory freed\n");
55- }
56- #endif
57-
58- TEC (numerical, test_comparisons) {
59- int a = 69 ;
60- int b = 420 ;
61- // TEC_ASSERT_GT(a, b); /* fails */
62- // TEC_ASSERT_GE(69, 420); /* fails */
63- TEC_ASSERT_LE (b, a);
64- TEC_ASSERT_LT (-5 , 0 );
65- TEC_ASSERT_GE (100 , 100 );
66- TEC_ASSERT_LE (-99 , -99 );
67- }
68-
69- TEC (float , precision) {
70- TEC_ASSERT_NEAR (0.1 + 0.2 , 0.3 , 3e-15 );
71- TEC_ASSERT_FLOAT_EQ (0.1 + 0.2 , 0.3 );
72- }
73-
74- TEC (mathutils, addition) {
75- const int five = 5 ;
76- TEC_ASSERT_EQ (add (2 , 3 ), five);
77- TEC_ASSERT_EQ (add (-10 , 5 ), -5 );
78-
79- TEC_ASSERT_NE (add (2 , 2 ), 5 );
80- }
81-
82- TEC (mathutils, numerical_comparisons) {
83- int five = multiply (5 , 1 );
84- int ten = multiply (5 , 2 );
85-
86- TEC_ASSERT (ten > five);
87- TEC_ASSERT (five < ten);
88- TEC_ASSERT (ten >= ten);
89- TEC_ASSERT (five <= five);
90- }
91-
92- TEC (mathutils, multiply) {
93- TEC_ASSERT_EQ (multiply (3 , 4 ), 12 );
94- TEC_ASSERT (multiply (5 , 2 ) % 2 == 0 );
95- TEC_ASSERT_NE (multiply (1 , 1 ), 0 );
96-
97- int result = multiply (-2 , 5 );
98- TEC_ASSERT_EQ (result, -10 );
99- TEC_ASSERT (result < 0 );
100- TEC_ASSERT_NE (result, 0 );
101-
102- result = multiply (0 , 100 );
103- TEC_ASSERT_EQ (result, 0 );
104- TEC_ASSERT (result <= 0 );
105- }
106-
107- TEC (mathutils, division) {
108- TEC_ASSERT_EQ (divide (10 , 2 ), 5 );
109- TEC_ASSERT (divide (9 , 3 ) > 0 );
110-
111- int result = divide (15 , 3 );
112- TEC_ASSERT_EQ (result, 5 );
113-
114- result = divide (0 , 5 );
115- TEC_ASSERT_EQ (result, 0 );
116-
117- // Edge case: divide by zero
118- result = divide (7 , 0 );
119- TEC_ASSERT_EQ (result, 0 );
120- }
121-
122- TEC (mathutils, factorial) {
123- TEC_ASSERT_EQ (factorial (0 ), 1 ); // Edge case: 0! = 1
124- TEC_ASSERT_EQ (factorial (5 ), 120 );
125- TEC_ASSERT_EQ (factorial (10 ), 3628800 );
126-
127- // Test for invalid input (negative numbers)
128- TEC_ASSERT_EQ (factorial (-5 ), 1 );
129- }
130-
131- TEC (logic, booleans_act_right) {
132- TEC_ASSERT (1 );
133- TEC_ASSERT (!0 );
134- TEC_ASSERT_EQ (1 == 1 , 1 );
135- TEC_ASSERT_NE (1 == 0 , 1 );
136- }
137-
138- #ifdef __cplusplus
139- #include < stdexcept>
140- #endif
14135
14236void function_that_throws () {
143- throw std::runtime_error (" Something went wrong! " );
37+ throw std::runtime_error (" This is an expected exception. " );
14438}
145-
14639void function_that_doesnt_throw () {}
14740
148- TEC (Exceptions, CatchesCorrectType ) {
41+ TEC (cpp_features, catches_correct_exception_type ) {
14942 TEC_ASSERT_THROWS (function_that_throws (), std::runtime_error);
15043}
151-
152- TEC (Exceptions, FailsOnWrongType) {
44+ TEC_XFAIL (cpp_features, fails_on_wrong_exception_type) {
15345 TEC_ASSERT_THROWS (function_that_throws (), std::invalid_argument);
15446}
155-
156- TEC (Exceptions, FailsWhenNoThrow) {
47+ TEC_XFAIL (cpp_features, fails_when_no_exception_is_thrown) {
15748 TEC_ASSERT_THROWS (function_that_doesnt_throw (), std::runtime_error);
15849}
15950
160- TEC_MAIN ()
51+ static int raii_sentry_counter = 0 ;
52+ struct RaiiSentry {
53+ RaiiSentry () { raii_sentry_counter++; }
54+ ~RaiiSentry () { raii_sentry_counter--; }
55+ };
56+
57+ TEC (cpp_features, assertion_failure_is_raii_safe) {
58+ TEC_ASSERT_EQ (raii_sentry_counter, 0 );
59+ try {
60+ RaiiSentry s;
61+ TEC_ASSERT_EQ (raii_sentry_counter, 1 );
62+ TEC_ASSERT_EQ (1 , 0 ); // This assertion throws
63+ } catch (...) {
64+ // THIS WILL PASS as the destructor would've restored the value so
65+ // this assertion will be logged in the passed one's
66+ TEC_ASSERT_EQ (raii_sentry_counter, 0 );
67+ // Exception was caught. If the destructor ran, the counter is 0.
68+ // Rethrow the exception so that:
69+ // 1) The test framework knows this block failed.
70+ // 2) Execution stops here and other assertions won't run
71+ // after this failure (cause that's the intended behavior).
72+ throw ;
73+ }
74+
75+ // THIS won't be logged cause we used throw in catch, this would've
76+ // logged if we forgot that throw;
77+ TEC_ASSERT_EQ (raii_sentry_counter, 0 );
78+ }
79+
80+ TEC_MAIN ();
0 commit comments