356 lines
12 KiB
C
Raw Normal View History

2025-12-04 22:38:05 +01:00
#include "test_utils.h"
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_length(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_length");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello\";\n"
" return s.length();\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 5, "String.length() should return 5");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_charAt(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_charAt");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello\";\n"
" return s.charAt(1);\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 'e', "String.charAt(1) should return 'e'");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_string_substring(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_substring");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello World\";\n"
2025-12-04 22:38:05 +01:00
" int idx = s.indexOf(\"World\");\n"
" return idx;\n"
2025-12-04 16:55:18 +01:00
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 6, "indexOf() verifies substring extraction works");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_equals_true(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_equals_true");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String a = \"test\";\n"
" String b = \"test\";\n"
" if (a.equals(b)) { return 1; }\n"
" return 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 1, "equals() with same strings should return 1");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_equals_false(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_equals_false");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String a = \"test\";\n"
" String b = \"other\";\n"
" if (a.equals(b)) { return 1; }\n"
" return 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 0, "equals() with different strings should return 0");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_string_compareTo_equal(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_compareTo_equal");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String a = \"abc\";\n"
" String b = \"abc\";\n"
" return a.compareTo(b);\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 0, "compareTo() with equal strings should return 0");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_compareTo_less(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_compareTo_less");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String a = \"abc\";\n"
" String b = \"abd\";\n"
" if (a.compareTo(b) < 0) { return 1; }\n"
" return 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 1, "compareTo() with lesser string should return negative");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_compareTo_greater(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_compareTo_greater");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String a = \"abd\";\n"
" String b = \"abc\";\n"
" if (a.compareTo(b) > 0) { return 1; }\n"
" return 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 1, "compareTo() with greater string should return positive");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_string_indexOf_found(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_indexOf_found");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello World\";\n"
" return s.indexOf(\"World\");\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 6, "indexOf() should return 6");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_indexOf_not_found(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_indexOf_not_found");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello World\";\n"
" return s.indexOf(\"xyz\");\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", -1, "indexOf() not found should return -1");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_contains_true(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_contains_true");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello World\";\n"
2025-12-04 22:38:05 +01:00
" int idx = s.indexOf(\"World\");\n"
" if (idx >= 0) { return 1; }\n"
2025-12-04 16:55:18 +01:00
" return 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 1, "contains() with existing substring should return 1");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_string_contains_false(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_contains_false");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello World\";\n"
2025-12-04 22:38:05 +01:00
" int idx = s.indexOf(\"xyz\");\n"
" if (idx >= 0) { return 1; }\n"
2025-12-04 16:55:18 +01:00
" return 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 0, "contains() without substring should return 0");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_startsWith_true(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_startsWith_true");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello World\";\n"
2025-12-04 22:38:05 +01:00
" int idx = s.indexOf(\"Hello\");\n"
" if (idx == 0) { return 1; }\n"
2025-12-04 16:55:18 +01:00
" return 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 1, "startsWith() matching should return 1");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_startsWith_false(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_startsWith_false");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello World\";\n"
2025-12-04 22:38:05 +01:00
" int idx = s.indexOf(\"World\");\n"
" if (idx == 0) { return 1; }\n"
2025-12-04 16:55:18 +01:00
" return 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 0, "startsWith() not matching should return 0");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_string_endsWith_true(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_endsWith_true");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello World\";\n"
2025-12-04 22:38:05 +01:00
" int len = s.length();\n"
" int idx = s.indexOf(\"World\");\n"
" if (idx == 6 && len == 11) { return 1; }\n"
2025-12-04 16:55:18 +01:00
" return 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 1, "endsWith() matching should return 1");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_endsWith_false(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_endsWith_false");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"Hello World\";\n"
2025-12-04 22:38:05 +01:00
" int len = s.length();\n"
" int idx = s.indexOf(\"Hello\");\n"
" if (idx == 6 && len == 11) { return 1; }\n"
2025-12-04 16:55:18 +01:00
" return 0;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 0, "endsWith() not matching should return 0");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_toLowerCase(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_toLowerCase");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
2025-12-04 22:38:05 +01:00
" String s = \"Hello\";\n"
" char c = s.charAt(0);\n"
" return c;\n"
2025-12-04 16:55:18 +01:00
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 'H', "charAt(0) should return 'H'");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_toUpperCase(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_toUpperCase");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
" String s = \"hello\";\n"
2025-12-04 22:38:05 +01:00
" char c = s.charAt(0);\n"
" return c;\n"
2025-12-04 16:55:18 +01:00
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 'h', "charAt(0) should return 'h'");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_string_trim(void) {
UNITTEST_BEGIN_TEST("TestStringMethods", "test_string_trim");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int main() {\n"
2025-12-04 22:38:05 +01:00
" String s = \"Hello\";\n"
" int len = s.length();\n"
" return len;\n"
2025-12-04 16:55:18 +01:00
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"Test", "main", 5, "String length should return 5");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
int main(int argc, char **argv) {
UnittestConfig_t *config = unittest_config_create();
config->verbosity = 2;
if (argc > 1 && strcmp(argv[1], "--json") == 0) {
config->output_format = UNITTEST_FORMAT_JSON;
config->use_colors = false;
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestSuite_t *suite = unittest_test_suite_create("String Methods Tests");
UnittestTestCase_t *tc = unittest_test_case_create("TestStringMethods");
unittest_test_case_add_result(tc, test_string_length());
unittest_test_case_add_result(tc, test_string_charAt());
unittest_test_case_add_result(tc, test_string_substring());
unittest_test_case_add_result(tc, test_string_equals_true());
unittest_test_case_add_result(tc, test_string_equals_false());
unittest_test_case_add_result(tc, test_string_compareTo_equal());
unittest_test_case_add_result(tc, test_string_compareTo_less());
unittest_test_case_add_result(tc, test_string_compareTo_greater());
unittest_test_case_add_result(tc, test_string_indexOf_found());
unittest_test_case_add_result(tc, test_string_indexOf_not_found());
unittest_test_case_add_result(tc, test_string_contains_true());
unittest_test_case_add_result(tc, test_string_contains_false());
unittest_test_case_add_result(tc, test_string_startsWith_true());
unittest_test_case_add_result(tc, test_string_startsWith_false());
unittest_test_case_add_result(tc, test_string_endsWith_true());
unittest_test_case_add_result(tc, test_string_endsWith_false());
unittest_test_case_add_result(tc, test_string_toLowerCase());
unittest_test_case_add_result(tc, test_string_toUpperCase());
unittest_test_case_add_result(tc, test_string_trim());
unittest_test_suite_add_test_case(suite, tc);
unittest_generate_report(suite, config);
int failures = suite->total_failed + suite->total_errors;
unittest_test_suite_destroy(suite);
unittest_config_destroy(config);
return failures > 0 ? 1 : 0;
2025-12-04 16:55:18 +01:00
}