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

Add double to string #169

Merged
merged 1 commit into from
Oct 17, 2023
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
1 change: 1 addition & 0 deletions include/utils/basic_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
double linear_interpolate(double *x_vector, long x_length, double *y_vector, long y_length, double x_value);
char* concat(const char *s1, ...);
void string_to_lowercase(char* str);
char* to_str(double x);

#endif //BASIC_FUNCTIONS_H
9 changes: 9 additions & 0 deletions src/utils/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ char* concat(const char *s1, ...) {
void string_to_lowercase(char* str) {
char *p;
for(p=str; *p; ++p) *p=tolower(*p);
}

char* to_str(double x) {
size_t l;
char *s;
l = (size_t)snprintf(NULL, 0, "%f", x) + 1;
s = (char*)malloc(l);
snprintf(s, l, "%.0f", x);
return s;
}
7 changes: 7 additions & 0 deletions tests/test_basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ int test_interpolate(){
TAIGA_ASSERT_ALMOST_EQ(15.4, y0, "decreasing order (case2)");
return TAIGA_ASSERT_SUMMARY();
}

int test_to_string(){
TAIGA_INIT_TEST("TO STRING");
TAIGA_ASSERT_COMPARE("50", to_str(50), "exact value 50");
TAIGA_ASSERT_COMPARE("49", to_str(49.2), "rounded value 49.2");
return TAIGA_ASSERT_SUMMARY();
}
1 change: 1 addition & 0 deletions tests/test_basic_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

int test_concat();
int test_interpolate();
int test_to_string();

#endif //TEST_BASIC_FUNCTIONS_H
1 change: 1 addition & 0 deletions tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
int main(){
return test_concat() +
test_interpolate() +
test_to_string() +
test_bspline() +
test_solver();
}
Expand Down
Loading