Skip to content

Commit

Permalink
Implemented test cases left
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Nov 5, 2024
1 parent 836cff2 commit 43b07ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ CARBON_API u32 carbon_crypto_crc32(const u8 *in, const usz in_size);
(i32) (actual), (i32) (expected))

#define carbon_should_be_f(expected, actual) \
CARBON_COMPARE(carbon_math_abs(expected - actual) > 1e-3, \
CARBON_COMPARE(carbon_math_abs(expected - actual) > 1e-6, \
"got '%f', expected '%f'", \
(f32) (actual), (f32) (expected))

Expand Down
1 change: 1 addition & 0 deletions src/carbon_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ f32 carbon_math_tanh(f32 x) {
}

f32 carbon_math_smoothstep(f32 a, f32 b, f32 t) {
CARBON_ASSERT(a < b);
f32 x = CARBON_CLAMP((t - a) / (b - a), 0, 1);
return x * x * (3 - 2 * x);
}
Expand Down
32 changes: 22 additions & 10 deletions test/carbon_math_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,44 @@ CARBON_TEST(carbon_math, abs) {
return CARBON_OK;
}

CARBON_TEST(carbon_math, exp) {
// CARBON_INFO("e^0 :: %.2f", carbon_math_exp(0));
// CARBON_INFO("e^1 :: %.2f", carbon_math_exp(1));
// CARBON_INFO("e^2 :: %.2f", carbon_math_exp(2));
return CARBON_KO;
}
/* CARBON_TEST(carbon_math, exp) { */
/* CARBON_INFO("e^0 :: %.2f", carbon_math_exp(0)); */
/* CARBON_INFO("e^1 :: %.2f", carbon_math_exp(1)); */
/* CARBON_INFO("e^2 :: %.2f", carbon_math_exp(2)); */
/* return CARBON_KO; */
/* } */

CARBON_TEST(carbon_math, sigmoid) {
carbon_should_be_f(0, carbon_math_sigmoid(-7));
carbon_should_be_f(0, carbon_math_sigmoid(-14));
carbon_should_be_f(0.006692, carbon_math_sigmoid(-5));
carbon_should_be_f(0.047425, carbon_math_sigmoid(-3));
carbon_should_be_f(0.268941, carbon_math_sigmoid(-1));
carbon_should_be_f(0.5, carbon_math_sigmoid(0));
carbon_should_be_f(0.731058, carbon_math_sigmoid(1));
carbon_should_be_f(0.952574, carbon_math_sigmoid(3));
carbon_should_be_f(0.993307, carbon_math_sigmoid(5));
carbon_should_be_f(1, carbon_math_sigmoid(7));
carbon_should_be_f(1, carbon_math_sigmoid(14));
return CARBON_OK;
}

CARBON_TEST(carbon_math, tanh) {
return CARBON_KO;
carbon_should_be_f(-1, carbon_math_tanh(-14));
carbon_should_be_f(-0.995054, carbon_math_tanh(-3));
carbon_should_be_f(-0.761594, carbon_math_tanh(-1));
carbon_should_be_f(0, carbon_math_tanh(0));
carbon_should_be_f(0.761594, carbon_math_tanh(1));
carbon_should_be_f(0.995054, carbon_math_tanh(3));
carbon_should_be_f(1, carbon_math_tanh(14));
return CARBON_OK;
}

CARBON_TEST(carbon_math, smoothstep) {
return CARBON_KO;
carbon_should_be_f(0, carbon_math_smoothstep(0, 1, 0));
carbon_should_be_f(0.028, carbon_math_smoothstep(0, 1, 0.1));
carbon_should_be_f(0.5, carbon_math_smoothstep(0, 1, 0.5));
carbon_should_be_f(0.784, carbon_math_smoothstep(0, 1, 0.7));
carbon_should_be_f(1, carbon_math_smoothstep(0, 1, 1));
return CARBON_OK;
}

CARBON_TEST(carbon_math, vec2_add) {
Expand Down

0 comments on commit 43b07ff

Please sign in to comment.