Skip to content

Commit

Permalink
Merge pull request #169 from taiga-project/feature-167
Browse files Browse the repository at this point in the history
Add double to string
  • Loading branch information
matyasaradi authored Oct 17, 2023
2 parents 2bebfef + 80abac6 commit 976f374
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 0 deletions.
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

0 comments on commit 976f374

Please sign in to comment.