diff --git a/include/utils/basic_functions.h b/include/utils/basic_functions.h index f60cdb5..e069c73 100644 --- a/include/utils/basic_functions.h +++ b/include/utils/basic_functions.h @@ -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 diff --git a/src/utils/basic_functions.c b/src/utils/basic_functions.c index 8ebc796..464be97 100644 --- a/src/utils/basic_functions.c +++ b/src/utils/basic_functions.c @@ -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; } \ No newline at end of file diff --git a/tests/test_basic_functions.c b/tests/test_basic_functions.c index 5349321..48ffb62 100644 --- a/tests/test_basic_functions.c +++ b/tests/test_basic_functions.c @@ -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(); +} \ No newline at end of file diff --git a/tests/test_basic_functions.h b/tests/test_basic_functions.h index bb858f1..edabfcb 100644 --- a/tests/test_basic_functions.h +++ b/tests/test_basic_functions.h @@ -3,5 +3,6 @@ int test_concat(); int test_interpolate(); +int test_to_string(); #endif //TEST_BASIC_FUNCTIONS_H diff --git a/tests/tests.c b/tests/tests.c index 9921cfa..ac17bb2 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -8,6 +8,7 @@ int main(){ return test_concat() + test_interpolate() + + test_to_string() + test_bspline() + test_solver(); }