Skip to content

Commit 5f22cab

Browse files
add more test cases
1 parent 50bcd79 commit 5f22cab

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

code/tests/cases/test_jellyfish.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,72 @@ FOSSIL_TEST_CASE(c_test_jellyfish_chain_hash) {
102102
ASSUME_ITS_TRUE(diff);
103103
}
104104

105+
FOSSIL_TEST_CASE(c_test_jellyfish_chain_save_and_load) {
106+
fossil_jellyfish_chain chain1, chain2;
107+
fossil_jellyfish_init(&chain1);
108+
fossil_jellyfish_init(&chain2);
109+
110+
fossil_jellyfish_learn(&chain1, "alpha", "beta");
111+
fossil_jellyfish_learn(&chain1, "gamma", "delta");
112+
113+
const char *filepath = "test_jellyfish_chain_save.dat";
114+
int save_result = fossil_jellyfish_save(&chain1, filepath);
115+
ASSUME_ITS_TRUE(save_result == 1);
116+
117+
int load_result = fossil_jellyfish_load(&chain2, filepath);
118+
ASSUME_ITS_TRUE(load_result == 1);
119+
120+
ASSUME_ITS_EQUAL_SIZE(chain2.count, 2);
121+
ASSUME_ITS_EQUAL_CSTR(chain2.memory[0].input, "alpha");
122+
ASSUME_ITS_EQUAL_CSTR(chain2.memory[0].output, "beta");
123+
ASSUME_ITS_EQUAL_CSTR(chain2.memory[1].input, "gamma");
124+
ASSUME_ITS_EQUAL_CSTR(chain2.memory[1].output, "delta");
125+
126+
// Clean up test file
127+
remove(filepath);
128+
}
129+
130+
FOSSIL_TEST_CASE(c_test_jellyfish_chain_save_fail) {
131+
fossil_jellyfish_chain chain;
132+
fossil_jellyfish_init(&chain);
133+
// Try to save to an invalid path
134+
int save_result = fossil_jellyfish_save(&chain, "/invalid/path/should_fail.dat");
135+
ASSUME_ITS_TRUE(save_result == 0);
136+
}
137+
138+
FOSSIL_TEST_CASE(c_test_jellyfish_chain_load_fail) {
139+
fossil_jellyfish_chain chain;
140+
fossil_jellyfish_init(&chain);
141+
// Try to load from a non-existent file
142+
int load_result = fossil_jellyfish_load(&chain, "nonexistent_file.dat");
143+
ASSUME_ITS_TRUE(load_result == 0);
144+
}
145+
146+
FOSSIL_TEST_CASE(c_test_jellyfish_reason_fuzzy) {
147+
fossil_jellyfish_chain chain;
148+
fossil_jellyfish_init(&chain);
149+
150+
fossil_jellyfish_learn(&chain, "cat", "meow");
151+
fossil_jellyfish_learn(&chain, "dog", "bark");
152+
fossil_jellyfish_learn(&chain, "bird", "tweet");
153+
154+
// Exact match
155+
const char *out1 = fossil_jellyfish_reason_fuzzy(&chain, "cat");
156+
ASSUME_ITS_EQUAL_CSTR(out1, "meow");
157+
158+
// Fuzzy match (one char off)
159+
const char *out2 = fossil_jellyfish_reason_fuzzy(&chain, "cot");
160+
ASSUME_ITS_EQUAL_CSTR(out2, "meow");
161+
162+
// Fuzzy match (closest)
163+
const char *out3 = fossil_jellyfish_reason_fuzzy(&chain, "bog");
164+
ASSUME_ITS_EQUAL_CSTR(out3, "bark");
165+
166+
// No close match
167+
const char *out4 = fossil_jellyfish_reason_fuzzy(&chain, "elephant");
168+
ASSUME_ITS_EQUAL_CSTR(out4, "Unknown");
169+
}
170+
105171
// * * * * * * * * * * * * * * * * * * * * * * * *
106172
// * Fossil Logic Test Pool
107173
// * * * * * * * * * * * * * * * * * * * * * * * *
@@ -112,6 +178,10 @@ FOSSIL_TEST_GROUP(c_jellyfish_tests) {
112178
FOSSIL_TEST_ADD(c_jellyfish_fixture, c_test_jellyfish_chain_cleanup);
113179
FOSSIL_TEST_ADD(c_jellyfish_fixture, c_test_jellyfish_chain_dump);
114180
FOSSIL_TEST_ADD(c_jellyfish_fixture, c_test_jellyfish_chain_hash);
181+
FOSSIL_TEST_ADD(c_jellyfish_fixture, c_test_jellyfish_chain_save_and_load);
182+
FOSSIL_TEST_ADD(c_jellyfish_fixture, c_test_jellyfish_chain_save_fail);
183+
FOSSIL_TEST_ADD(c_jellyfish_fixture, c_test_jellyfish_chain_load_fail);
184+
FOSSIL_TEST_ADD(c_jellyfish_fixture, c_test_jellyfish_reason_fuzzy);
115185

116186
FOSSIL_TEST_REGISTER(c_jellyfish_fixture);
117187
} // end of tests

code/tests/cases/test_jellyfish.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,58 @@ FOSSIL_TEST_CASE(cpp_test_jellyfishai_chain_hash) {
100100
ASSUME_ITS_TRUE(diff);
101101
}
102102

103+
FOSSIL_TEST_CASE(cpp_test_jellyfishai_reason_fuzzy_exact_and_fuzzy) {
104+
JellyfishAI ai;
105+
ai.learn("apple", "fruit");
106+
ai.learn("appl", "not fruit");
107+
108+
// Exact match
109+
std::string out1 = ai.reason_fuzzy("apple");
110+
ASSUME_ITS_EQUAL_CSTR(out1.c_str(), "fruit");
111+
112+
// Fuzzy match (missing 'e')
113+
std::string out2 = ai.reason_fuzzy("appl");
114+
// Accept either "not fruit" or "fruit" depending on fuzzy logic
115+
ASSUME_ITS_TRUE(out2 == "not fruit" || out2 == "fruit");
116+
117+
// Fuzzy match (typo)
118+
std::string out3 = ai.reason_fuzzy("aple");
119+
ASSUME_ITS_TRUE(out3 == "fruit" || out3 == "not fruit");
120+
121+
// No match
122+
std::string out4 = ai.reason_fuzzy("banana");
123+
ASSUME_ITS_EQUAL_CSTR(out4.c_str(), "Unknown");
124+
}
125+
126+
FOSSIL_TEST_CASE(cpp_test_jellyfishai_save_and_load) {
127+
JellyfishAI ai;
128+
ai.learn("cat", "meow");
129+
ai.learn("dog", "bark");
130+
131+
const std::string filepath = "jellyfish_chain_test_save.dat";
132+
int save_result = ai.save(filepath);
133+
ASSUME_ITS_EQUAL_I32(save_result, 0);
134+
135+
JellyfishAI ai2;
136+
int load_result = ai2.load(filepath);
137+
ASSUME_ITS_EQUAL_I32(load_result, 0);
138+
139+
// Check that loaded data matches
140+
std::string out1 = ai2.reason("cat");
141+
std::string out2 = ai2.reason("dog");
142+
ASSUME_ITS_EQUAL_CSTR(out1.c_str(), "meow");
143+
ASSUME_ITS_EQUAL_CSTR(out2.c_str(), "bark");
144+
145+
// Cleanup test file
146+
std::remove(filepath.c_str());
147+
}
148+
149+
FOSSIL_TEST_CASE(cpp_test_jellyfishai_load_nonexistent_file) {
150+
JellyfishAI ai;
151+
int result = ai.load("nonexistent_file_hopefully_12345.dat");
152+
ASSUME_ITS_TRUE(result != 0);
153+
}
154+
103155
// * * * * * * * * * * * * * * * * * * * * * * * *
104156
// * Fossil Logic Test Pool
105157
// * * * * * * * * * * * * * * * * * * * * * * * *
@@ -110,6 +162,9 @@ FOSSIL_TEST_GROUP(cpp_jellyfish_tests) {
110162
FOSSIL_TEST_ADD(cpp_jellyfish_fixture, cpp_test_jellyfishai_chain_cleanup);
111163
FOSSIL_TEST_ADD(cpp_jellyfish_fixture, cpp_test_jellyfishai_chain_dump);
112164
FOSSIL_TEST_ADD(cpp_jellyfish_fixture, cpp_test_jellyfishai_chain_hash);
165+
FOSSIL_TEST_ADD(cpp_jellyfish_fixture, cpp_test_jellyfishai_save_and_load);
166+
FOSSIL_TEST_ADD(cpp_jellyfish_fixture, cpp_test_jellyfishai_load_nonexistent_file);
167+
FOSSIL_TEST_ADD(cpp_jellyfish_fixture, cpp_test_jellyfishai_reason_fuzzy_exact_and_fuzzy);
113168

114169
FOSSIL_TEST_REGISTER(cpp_jellyfish_fixture);
115170
} // end of tests

0 commit comments

Comments
 (0)