Skip to content
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

Improve fixed point tests + code formatting #42

Merged
merged 2 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions examples/c/fixed_point/fixed_point_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,42 @@ static void double_to_fixed16_test(__attribute__((unused)) void** state)
fixed_point_t output_round = double_to_fixed_round(11.5);
fixed_point_t output_truncate = double_to_fixed_truncate(11.5);
assert_int_equal(0x170, output_truncate);
assert_int_equal(output_round, output_truncate); // No difference should occur here
assert_int_equal(output_round, output_truncate);

output_round = double_to_fixed_round(128);
output_truncate = double_to_fixed_truncate(128);
assert_int_equal(0x1000, output_truncate);
assert_int_equal(output_round, output_truncate); // No difference should occur here
assert_int_equal(output_round, output_truncate);

output_round = double_to_fixed_round(-128);
output_truncate = double_to_fixed_truncate(-128);
assert_int_equal(-0x1000, output_truncate);
assert_int_equal(output_round, output_truncate); // No difference should occur here
assert_int_equal((int16_t)0xf000, output_truncate);
assert_int_equal(output_round, output_truncate);

output_round = double_to_fixed_round(128.28);
output_truncate = double_to_fixed_truncate(128.28);
assert_int_equal(0x1009, output_round);
// Here, truncate loses precision vs round
assert_int_equal(0x1008, output_truncate);

output_round = double_to_fixed_round(-64.28);
output_truncate = double_to_fixed_truncate(-64.28);
assert_int_equal((int16_t)0xf7f7, output_round);
// Here, truncate loses precision vs round
assert_int_equal((int16_t)0xf7f8, output_truncate);
}

static void fixed16_to_double_test(__attribute__((unused)) void** state)
{
double output = fixed_to_double(0x1000);
assert_float_equal(128.0, output, 0.01);

output = fixed_to_double(-0x1000);
output = fixed_to_double(0xf000);
assert_float_equal(-128.0, output, 0.01);

output = fixed_to_double(0xf7f7);
assert_float_equal(-64.28125, output, 0.01);

output = fixed_to_double(0x1009);
assert_float_equal(128.28125, output, 0.01);

Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/endian/endian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ constexpr typename std::enable_if<std::is_unsigned<T>::value, T>::type
byteswap(T i, T j = 0u, std::size_t n = 0u) noexcept
{
return n == sizeof(T) ?
j :
j :
byteswap<T>(i >> CHAR_BIT,
static_cast<T>((j << CHAR_BIT) |
(i & static_cast<T>(static_cast<unsigned char>(-1)))),
Expand Down
3 changes: 2 additions & 1 deletion examples/libc/stdlib/abs.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@

#include <stdlib.h>

int abs(j) int j;
int abs(j)
int j;
{
return (j < 0 ? -j : j);
}
3 changes: 2 additions & 1 deletion examples/libc/stdlib/div.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

#include <stdlib.h> /* div_t */

div_t div(num, denom) int num, denom;
div_t div(num, denom)
int num, denom;
{
div_t r;

Expand Down
3 changes: 2 additions & 1 deletion examples/libc/stdlib/labs.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@

#include <stdlib.h>

long labs(j) long j;
long labs(j)
long j;
{
return (j < 0 ? -j : j);
}
3 changes: 2 additions & 1 deletion examples/libc/stdlib/ldiv.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

#include <stdlib.h> /* ldiv_t */

ldiv_t ldiv(num, denom) long num, denom;
ldiv_t ldiv(num, denom)
long num, denom;
{
ldiv_t r;

Expand Down