Skip to content

Commit c7cbbc0

Browse files
DreamPearlscopeInfinity
authored andcommitted
Add unit test for stdlib.c
1 parent 57e90fc commit c7cbbc0

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/usr/local/src/test-stdlib.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <testing.h>
5+
6+
void test_min() {
7+
ASSERT_TRUE(std::min(3, 6) == 3);
8+
ASSERT_TRUE(std::min(6, 4) == 4);
9+
ASSERT_TRUE(std::min(300, 6) == 6);
10+
}
11+
12+
void test_max() {
13+
ASSERT_TRUE(std::max(3, 6) == 6);
14+
ASSERT_TRUE(std::max(8, 4) == 8);
15+
ASSERT_TRUE(std::max(3, 600) == 600);
16+
}
17+
18+
void test_abs() {
19+
ASSERT_TRUE(std::abs(0) == 0);
20+
ASSERT_TRUE(std::abs(3) == 3);
21+
ASSERT_TRUE(std::abs(-3) == 3);
22+
}
23+
24+
void test_atoi() { ASSERT_TRUE(std::atoi("36") == 36); }
25+
26+
void test_itoa() {
27+
char buffer[20];
28+
std::itoa(54325, buffer, 2);
29+
ASSERT_TRUE(std::strcmp(buffer, "1101010000110101") == 0);
30+
std::itoa(54325, buffer, 10);
31+
ASSERT_TRUE(std::strcmp(buffer, "54325") == 0);
32+
std::itoa(54325, buffer, 16);
33+
ASSERT_TRUE(std::strcmp(buffer, "D435") == 0);
34+
}
35+
36+
void test_ftoa() {
37+
char buffer[20];
38+
std::ftoa(1.555, buffer, 1);
39+
ASSERT_TRUE(std::strcmp(buffer, "0.1e+1") == 0);
40+
std::ftoa(1.555, buffer, 2);
41+
ASSERT_TRUE(std::strcmp(buffer, "0.15e+1") == 0);
42+
std::ftoa(-1.555, buffer, 2);
43+
ASSERT_TRUE(std::strcmp(buffer, "-0.15e+1") == 0);
44+
std::ftoa(0.01555, buffer, 2);
45+
ASSERT_TRUE(std::strcmp(buffer, "0.15e-1") == 0);
46+
std::ftoa(0.1555, buffer, 2);
47+
ASSERT_TRUE(std::strcmp(buffer, "0.15") == 0);
48+
}
49+
50+
int main(int argc, char *argv[]) {
51+
TEST_INIT();
52+
53+
RUN_TEST(test_min);
54+
RUN_TEST(test_max);
55+
RUN_TEST(test_abs);
56+
RUN_TEST(test_atoi);
57+
RUN_TEST(test_itoa);
58+
RUN_TEST(test_ftoa);
59+
60+
TEST_SUMMARY();
61+
return 0;
62+
}

tests/app/libraries/usr_lib_test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ function add_library_unit_test {
1212
}
1313

1414
add_library_unit_test "test-string"
15+
add_library_unit_test "test-stdlib"

0 commit comments

Comments
 (0)