From ccea6fa5fc1fbc5fe001b26712f6b5d83821fe44 Mon Sep 17 00:00:00 2001 From: Steve Morley Date: Fri, 20 Sep 2024 13:48:53 -0600 Subject: [PATCH 1/2] Add unit tests for string manipulation and clean some whitespace --- libLanlGeoMag/Lgm_CTrans.c | 4 +- libLanlGeoMag/Lgm_Misc.c | 33 ------------ tests/Makefile.am | 8 ++- tests/check_string_funcs.c | 103 +++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 37 deletions(-) create mode 100644 tests/check_string_funcs.c diff --git a/libLanlGeoMag/Lgm_CTrans.c b/libLanlGeoMag/Lgm_CTrans.c index 303be8743..f5857bafc 100644 --- a/libLanlGeoMag/Lgm_CTrans.c +++ b/libLanlGeoMag/Lgm_CTrans.c @@ -2331,7 +2331,7 @@ int MonthStrToNum( char *str ) { char *Lgm_StrToLower( char *str, int nmax ) { int n = 0; char *p = str; - while ( (p != '\0' ) && (n +#include "../libLanlGeoMag/Lgm/Lgm_Misc.h" + +/*BEGIN String case test case*/ + +START_TEST(test_StrToLower) { + + char* Result; + char TestString[128]; + + printf("Starting test_StrToUpper\n"); + sprintf(TestString, "AN_UPPER_CASE_STRING"); + printf("Converting string to lower: %s\n", TestString); + Result = Lgm_StrToLower(TestString, 128); + printf("Result: %s\n\n", TestString); + fflush(stdout); + fail_unless(!strcmp(TestString, "an_upper_case_string")); + return; +} +END_TEST + +START_TEST(test_StrToUpper) { + + char* Result; + char TestString[128]; + + printf("Starting test_StrToUpper\n"); + sprintf(TestString, "a_lower_case_string"); + printf("Converting string to upper: %s\n", TestString); + Result = Lgm_StrToUpper(TestString, 128); + printf("Result: %s\n\n", TestString); + fflush(stdout); + fail_unless(!strcmp(TestString, "A_LOWER_CASE_STRING")); + return; +} +END_TEST + +START_TEST(test_ReplaceSubString) { + + int Result; + char TestString[128], NewString[128], Substr[5]; + + printf("Starting test_ReplaceSubString\n"); + sprintf(Substr, "%02d", 4); + sprintf(TestString, "2024-%MM-01T12:00:00Z"); + printf("Replacing wildcard substring: %s\n", TestString); + Lgm_ReplaceSubString(NewString, TestString, "%MM", Substr); + printf("Result: %s\n\n", NewString); + fflush(stdout); + fail_unless(!strcmp(NewString, "2024-04-01T12:00:00Z")); + return; +} +END_TEST + +START_TEST(test_ReplaceSubString2) { + + char TestString[128], Substr[5]; + char* NewString; + + printf("Starting test_ReplaceSubString2\n"); + sprintf(Substr, "%02d", 4); + sprintf(TestString, "2024-%MM-01T12:00:00Z"); + printf("Replacing wildcard substring: %s\n", TestString); + Lgm_ReplaceSubString2(&NewString, TestString, "%MM", Substr); + printf("Result: %s\n\n", NewString); + fflush(stdout); + fail_unless(!strcmp(NewString, "2024-04-01T12:00:00Z")); + free(NewString); + return; +} +END_TEST + + +Suite *ManipStr_suite(void) { + + Suite *s = suite_create("STRING_FUNCTION_TESTS"); + + TCase *tc_manipStr = tcase_create("ISO Time Parser"); + tcase_add_test(tc_manipStr, test_StrToLower); + tcase_add_test(tc_manipStr, test_StrToUpper); + tcase_add_test(tc_manipStr, test_ReplaceSubString); + tcase_add_test(tc_manipStr, test_ReplaceSubString2); + suite_add_tcase(s, tc_manipStr); + + return s; + +} + +int main(void) { + + int number_failed; + Suite *s = ManipStr_suite(); + SRunner *sr = srunner_create(s); + + printf("\n\n"); + srunner_run_all(sr, CK_ENV); + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); + + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; + +} From aa628438c040f0e4458569a79023c0908abb2710 Mon Sep 17 00:00:00 2001 From: Steve Morley Date: Fri, 20 Sep 2024 13:58:12 -0600 Subject: [PATCH 2/2] move Lgm_StrToUppper and Lgm_StrToLower to Lgm_Misc Other string handling is already in Lgm_Misc, so this is a logical grouping. --- libLanlGeoMag/Lgm/Lgm_CTrans.h | 2 -- libLanlGeoMag/Lgm/Lgm_Misc.h | 2 ++ libLanlGeoMag/Lgm_CTrans.c | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/libLanlGeoMag/Lgm/Lgm_CTrans.h b/libLanlGeoMag/Lgm/Lgm_CTrans.h index e513f3563..e0ada3ecb 100644 --- a/libLanlGeoMag/Lgm/Lgm_CTrans.h +++ b/libLanlGeoMag/Lgm/Lgm_CTrans.h @@ -855,8 +855,6 @@ void Lgm_GEOD_to_WGS84( double GeodLat, double GeodLong, double GeodHiegh void Lgm_Nutation( double T_TT, double nTerms, double *dPSi, double *dEps ); int IsoTimeStringToDateTime( char *TimeString, Lgm_DateTime *d, Lgm_CTrans *c ); int MonthStrToNum( char *str ); -char *Lgm_StrToLower( char *str, int nmax ); -char *Lgm_StrToUpper( char *str, int nmax ); /* * Leap Second Related Routines diff --git a/libLanlGeoMag/Lgm/Lgm_Misc.h b/libLanlGeoMag/Lgm/Lgm_Misc.h index 1710b15cc..ae1f0894a 100644 --- a/libLanlGeoMag/Lgm/Lgm_Misc.h +++ b/libLanlGeoMag/Lgm/Lgm_Misc.h @@ -8,5 +8,7 @@ void Lgm_ReplaceSubString2( char **OutStr, char *OrigStr, char *SubStr, char *RepStr ); void Lgm_ReplaceSubString( char *OutStr, char *OrigStr, char *SubStr, char *RepStr ); +char *Lgm_StrToLower( char *str, int nmax ); +char *Lgm_StrToUpper( char *str, int nmax ); #endif diff --git a/libLanlGeoMag/Lgm_CTrans.c b/libLanlGeoMag/Lgm_CTrans.c index f5857bafc..5bd3f28b2 100644 --- a/libLanlGeoMag/Lgm_CTrans.c +++ b/libLanlGeoMag/Lgm_CTrans.c @@ -58,6 +58,7 @@ */ #include "Lgm/Lgm_CTrans.h" #include "Lgm/Lgm_Quat.h" +#include "Lgm/Lgm_Misc.h" #include "config.h" #include