Skip to content

Commit 354c7e5

Browse files
add test cases for iochat and nlp
1 parent 77a013a commit 354c7e5

File tree

2 files changed

+330
-0
lines changed

2 files changed

+330
-0
lines changed

code/tests/cases/test_iochat.c

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop high-
6+
* performance, cross-platform applications and libraries. The code contained
7+
* herein is subject to the terms and conditions defined in the project license.
8+
*
9+
* Author: Michael Gene Brockus (Dreamer)
10+
*
11+
* Copyright (C) 2024 Fossil Logic. All rights reserved.
12+
* -----------------------------------------------------------------------------
13+
*/
14+
#include <fossil/pizza/framework.h>
15+
#include "fossil/ai/framework.h"
16+
17+
18+
// * * * * * * * * * * * * * * * * * * * * * * * *
19+
// * Fossil Logic Test Utilities
20+
// * * * * * * * * * * * * * * * * * * * * * * * *
21+
// Setup steps for things like test fixtures and
22+
// mock objects are set here.
23+
// * * * * * * * * * * * * * * * * * * * * * * * *
24+
25+
FOSSIL_TEST_SUITE(c_iochat_fixture);
26+
27+
FOSSIL_SETUP(c_iochat_fixture) {
28+
// Setup the test fixture
29+
}
30+
31+
FOSSIL_TEARDOWN(c_iochat_fixture) {
32+
// Teardown the test fixture
33+
}
34+
35+
// * * * * * * * * * * * * * * * * * * * * * * * *
36+
// * Fossil Logic Test Cases
37+
// * * * * * * * * * * * * * * * * * * * * * * * *
38+
// The test cases below are provided as samples, inspired
39+
// by the Meson build system's approach of using test cases
40+
// as samples for library usage.
41+
// * * * * * * * * * * * * * * * * * * * * * * * *
42+
43+
FOSSIL_TEST_CASE(c_test_iochat_start_and_end_session) {
44+
fossil_jellyfish_chain_t chain;
45+
fossil_jellyfish_init(&chain);
46+
47+
int start_result = fossil_io_chat_start("test_session", &chain);
48+
ASSUME_ITS_EQUAL_I32(start_result, 0);
49+
50+
int end_result = fossil_io_chat_end(&chain);
51+
ASSUME_ITS_EQUAL_I32(end_result, 0);
52+
}
53+
54+
FOSSIL_TEST_CASE(c_test_iochat_respond_known_and_unknown) {
55+
fossil_jellyfish_chain_t chain;
56+
fossil_jellyfish_init(&chain);
57+
58+
fossil_io_chat_start("chat", &chain);
59+
fossil_io_chat_learn_response(&chain, "hi", "hello there!");
60+
61+
char output[64] = {0};
62+
int found = fossil_io_chat_respond(&chain, "hi", output, sizeof(output));
63+
ASSUME_ITS_EQUAL_I32(found, 0);
64+
ASSUME_ITS_EQUAL_CSTR(output, "hello there!");
65+
66+
int not_found = fossil_io_chat_respond(&chain, "unknown", output, sizeof(output));
67+
ASSUME_ITS_EQUAL_I32(not_found, -1);
68+
ASSUME_ITS_TRUE(strstr(output, "not sure") != NULL);
69+
70+
fossil_io_chat_end(&chain);
71+
}
72+
73+
FOSSIL_TEST_CASE(c_test_iochat_inject_system_message_and_immutable) {
74+
fossil_jellyfish_chain_t chain;
75+
fossil_jellyfish_init(&chain);
76+
77+
int result = fossil_io_chat_inject_system_message(&chain, "System Ready");
78+
ASSUME_ITS_EQUAL_I32(result, 0);
79+
80+
// Last block should be immutable and input "[system]"
81+
size_t idx = chain.count - 1;
82+
ASSUME_ITS_TRUE(chain.memory[idx].attributes.immutable);
83+
ASSUME_ITS_EQUAL_CSTR(chain.memory[idx].io.input, "[system]");
84+
ASSUME_ITS_EQUAL_CSTR(chain.memory[idx].io.output, "System Ready");
85+
}
86+
87+
FOSSIL_TEST_CASE(c_test_iochat_learn_response_and_turn_count) {
88+
fossil_jellyfish_chain_t chain;
89+
fossil_jellyfish_init(&chain);
90+
91+
fossil_io_chat_learn_response(&chain, "foo", "bar");
92+
fossil_io_chat_learn_response(&chain, "baz", "qux");
93+
94+
int turns = fossil_io_chat_turn_count(&chain);
95+
ASSUME_ITS_EQUAL_I32(turns, 2);
96+
}
97+
98+
FOSSIL_TEST_CASE(c_test_iochat_summarize_session_basic) {
99+
fossil_jellyfish_chain_t chain;
100+
fossil_jellyfish_init(&chain);
101+
102+
fossil_io_chat_learn_response(&chain, "hello", "world");
103+
fossil_io_chat_learn_response(&chain, "how are you", "fine");
104+
105+
char summary[256] = {0};
106+
int result = fossil_io_chat_summarize_session(&chain, summary, sizeof(summary));
107+
ASSUME_ITS_EQUAL_I32(result, 0);
108+
ASSUME_ITS_TRUE(strstr(summary, "hello") != NULL);
109+
ASSUME_ITS_TRUE(strstr(summary, "world") != NULL);
110+
ASSUME_ITS_TRUE(strstr(summary, "how are you") != NULL);
111+
}
112+
113+
FOSSIL_TEST_CASE(c_test_iochat_filter_recent_turns) {
114+
fossil_jellyfish_chain_t chain, filtered;
115+
fossil_jellyfish_init(&chain);
116+
fossil_jellyfish_init(&filtered);
117+
118+
fossil_io_chat_learn_response(&chain, "a", "1");
119+
fossil_io_chat_learn_response(&chain, "b", "2");
120+
fossil_io_chat_learn_response(&chain, "c", "3");
121+
122+
int result = fossil_io_chat_filter_recent(&chain, &filtered, 2);
123+
ASSUME_ITS_EQUAL_I32(result, 0);
124+
ASSUME_ITS_EQUAL_I32(filtered.count, 2);
125+
ASSUME_ITS_EQUAL_CSTR(filtered.memory[0].io.input, "b");
126+
ASSUME_ITS_EQUAL_CSTR(filtered.memory[1].io.input, "c");
127+
}
128+
129+
FOSSIL_TEST_CASE(c_test_iochat_export_and_import_history) {
130+
fossil_jellyfish_chain_t chain, imported;
131+
fossil_jellyfish_init(&chain);
132+
fossil_jellyfish_init(&imported);
133+
134+
fossil_io_chat_learn_response(&chain, "x", "y");
135+
fossil_io_chat_learn_response(&chain, "z", "w");
136+
137+
const char *filepath = "test_iochat_history.txt";
138+
int export_result = fossil_io_chat_export_history(&chain, filepath);
139+
ASSUME_ITS_EQUAL_I32(export_result, 0);
140+
141+
int import_result = fossil_io_chat_import_context(&imported, filepath);
142+
ASSUME_ITS_EQUAL_I32(import_result, 0);
143+
144+
ASSUME_ITS_EQUAL_I32(imported.count, 2);
145+
ASSUME_ITS_EQUAL_CSTR(imported.memory[0].io.input, "x");
146+
ASSUME_ITS_EQUAL_CSTR(imported.memory[0].io.output, "y");
147+
ASSUME_ITS_EQUAL_CSTR(imported.memory[1].io.input, "z");
148+
ASSUME_ITS_EQUAL_CSTR(imported.memory[1].io.output, "w");
149+
150+
remove(filepath);
151+
}
152+
153+
// * * * * * * * * * * * * * * * * * * * * * * * *
154+
// * Fossil Logic Test Pool
155+
// * * * * * * * * * * * * * * * * * * * * * * * *
156+
FOSSIL_TEST_GROUP(c_iochat_tests) {
157+
FOSSIL_TEST_ADD(c_iochat_fixture, c_test_iochat_start_and_end_session);
158+
FOSSIL_TEST_ADD(c_iochat_fixture, c_test_iochat_respond_known_and_unknown);
159+
FOSSIL_TEST_ADD(c_iochat_fixture, c_test_iochat_inject_system_message_and_immutable);
160+
FOSSIL_TEST_ADD(c_iochat_fixture, c_test_iochat_learn_response_and_turn_count);
161+
FOSSIL_TEST_ADD(c_iochat_fixture, c_test_iochat_summarize_session_basic);
162+
FOSSIL_TEST_ADD(c_iochat_fixture, c_test_iochat_filter_recent_turns);
163+
FOSSIL_TEST_ADD(c_iochat_fixture, c_test_iochat_export_and_import_history);
164+
165+
FOSSIL_TEST_REGISTER(c_iochat_fixture);
166+
} // end of tests

code/tests/cases/test_lang.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop high-
6+
* performance, cross-platform applications and libraries. The code contained
7+
* herein is subject to the terms and conditions defined in the project license.
8+
*
9+
* Author: Michael Gene Brockus (Dreamer)
10+
*
11+
* Copyright (C) 2024 Fossil Logic. All rights reserved.
12+
* -----------------------------------------------------------------------------
13+
*/
14+
#include <fossil/pizza/framework.h>
15+
#include "fossil/ai/framework.h"
16+
17+
18+
// * * * * * * * * * * * * * * * * * * * * * * * *
19+
// * Fossil Logic Test Utilities
20+
// * * * * * * * * * * * * * * * * * * * * * * * *
21+
// Setup steps for things like test fixtures and
22+
// mock objects are set here.
23+
// * * * * * * * * * * * * * * * * * * * * * * * *
24+
25+
FOSSIL_TEST_SUITE(c_lang_fixture);
26+
27+
FOSSIL_SETUP(c_lang_fixture) {
28+
// Setup the test fixture
29+
}
30+
31+
FOSSIL_TEARDOWN(c_lang_fixture) {
32+
// Teardown the test fixture
33+
}
34+
35+
// * * * * * * * * * * * * * * * * * * * * * * * *
36+
// * Fossil Logic Test Cases
37+
// * * * * * * * * * * * * * * * * * * * * * * * *
38+
// The test cases below are provided as samples, inspired
39+
// by the Meson build system's approach of using test cases
40+
// as samples for library usage.
41+
// * * * * * * * * * * * * * * * * * * * * * * * *
42+
43+
FOSSIL_TEST_CASE(c_test_lang_tokenize_basic) {
44+
char tokens[8][FOSSIL_JELLYFISH_TOKEN_SIZE] = {0};
45+
size_t n = fossil_lang_tokenize("Hello, world! This is a test.", tokens, 8);
46+
ASSUME_ITS_EQUAL_I32(n, 6);
47+
ASSUME_ITS_EQUAL_CSTR(tokens[0], "hello");
48+
ASSUME_ITS_EQUAL_CSTR(tokens[1], "world");
49+
ASSUME_ITS_EQUAL_CSTR(tokens[2], "this");
50+
ASSUME_ITS_EQUAL_CSTR(tokens[3], "is");
51+
ASSUME_ITS_EQUAL_CSTR(tokens[4], "a");
52+
ASSUME_ITS_EQUAL_CSTR(tokens[5], "test");
53+
}
54+
55+
FOSSIL_TEST_CASE(c_test_lang_is_question) {
56+
ASSUME_ITS_TRUE(fossil_lang_is_question("Is this a question?"));
57+
ASSUME_ITS_TRUE(fossil_lang_is_question("What time is it"));
58+
ASSUME_ITS_TRUE(fossil_lang_is_question("Could you help me?"));
59+
ASSUME_ITS_TRUE(!fossil_lang_is_question("This is not a question."));
60+
}
61+
62+
FOSSIL_TEST_CASE(c_test_lang_detect_emotion) {
63+
float pos = fossil_lang_detect_emotion("I love this!");
64+
float neg = fossil_lang_detect_emotion("This is terrible.");
65+
float neu = fossil_lang_detect_emotion("The sky is blue.");
66+
ASSUME_ITS_TRUE(pos > 0.5f);
67+
ASSUME_ITS_TRUE(neg < -0.5f);
68+
ASSUME_ITS_TRUE(neu > -0.2f && neu < 0.2f);
69+
}
70+
71+
FOSSIL_TEST_CASE(c_test_lang_detect_bias_or_falsehood) {
72+
ASSUME_ITS_EQUAL_I32(fossil_lang_detect_bias_or_falsehood("Everyone knows this is the best!"), 1);
73+
ASSUME_ITS_EQUAL_I32(fossil_lang_detect_bias_or_falsehood("The sun rises in the east."), 0);
74+
}
75+
76+
FOSSIL_TEST_CASE(c_test_lang_align_truth) {
77+
fossil_jellyfish_chain_t chain;
78+
fossil_jellyfish_init(&chain);
79+
fossil_io_chat_learn_response(&chain, "The sky is blue.", "Yes, it is.");
80+
int aligned = fossil_lang_align_truth(&chain, "The sky is blue.");
81+
int unknown = fossil_lang_align_truth(&chain, "Grass is purple.");
82+
ASSUME_ITS_EQUAL_I32(aligned, 1);
83+
ASSUME_ITS_TRUE(unknown == 0 || unknown == -1);
84+
}
85+
86+
FOSSIL_TEST_CASE(c_test_lang_similarity) {
87+
float sim1 = fossil_lang_similarity("The quick brown fox", "A quick brown fox");
88+
float sim2 = fossil_lang_similarity("cat", "dog");
89+
float sim3 = fossil_lang_similarity("identical", "identical");
90+
ASSUME_ITS_TRUE(sim1 > 0.5f);
91+
ASSUME_ITS_TRUE(sim2 < 0.5f);
92+
ASSUME_ITS_TRUE(sim3 > 0.99f);
93+
}
94+
95+
FOSSIL_TEST_CASE(c_test_lang_summarize_and_normalize) {
96+
char summary[64] = {0};
97+
char normalized[64] = {0};
98+
fossil_lang_summarize("This is a very long sentence that should be summarized.", summary, sizeof(summary));
99+
fossil_lang_normalize("I'm gonna win!", normalized, sizeof(normalized));
100+
ASSUME_ITS_TRUE(strlen(summary) > 0);
101+
ASSUME_ITS_TRUE(strstr(normalized, "going to") != NULL);
102+
}
103+
104+
FOSSIL_TEST_CASE(c_test_lang_extract_focus) {
105+
char focus[32] = {0};
106+
fossil_lang_extract_focus("The quick brown fox jumps over the lazy dog.", focus, sizeof(focus));
107+
ASSUME_ITS_TRUE(strlen(focus) > 0);
108+
// Heuristic: focus word should be "fox" or "dog"
109+
ASSUME_ITS_TRUE(strstr(focus, "fox") != NULL || strstr(focus, "dog") != NULL);
110+
}
111+
112+
FOSSIL_TEST_CASE(c_test_lang_estimate_trust) {
113+
fossil_jellyfish_chain_t chain;
114+
fossil_jellyfish_init(&chain);
115+
float trust1 = fossil_lang_estimate_trust(&chain, "The sky is blue.");
116+
float trust2 = fossil_lang_estimate_trust(&chain, "Everyone knows this is the best!");
117+
ASSUME_ITS_TRUE(trust1 > trust2);
118+
ASSUME_ITS_TRUE(trust1 >= 0.0f && trust1 <= 1.0f);
119+
}
120+
121+
FOSSIL_TEST_CASE(c_test_lang_embedding_similarity) {
122+
float a[4] = {1.0f, 0.0f, 0.0f, 0.0f};
123+
float b[4] = {1.0f, 0.0f, 0.0f, 0.0f};
124+
float c[4] = {0.0f, 1.0f, 0.0f, 0.0f};
125+
float sim_ab = fossil_lang_embedding_similarity(a, b, 4);
126+
float sim_ac = fossil_lang_embedding_similarity(a, c, 4);
127+
ASSUME_ITS_TRUE(sim_ab > 0.99f);
128+
ASSUME_ITS_TRUE(sim_ac < 0.1f);
129+
}
130+
131+
FOSSIL_TEST_CASE(c_test_lang_generate_variants) {
132+
char variants[3][256] = {{0}};
133+
fossil_lang_generate_variants("hello", variants, 3);
134+
ASSUME_ITS_TRUE(strlen(variants[0]) > 0);
135+
// At least one variant should differ from input
136+
ASSUME_ITS_TRUE(strcmp(variants[0], "hello") == 0 || strcmp(variants[1], "hello") == 0);
137+
}
138+
139+
FOSSIL_TEST_CASE(c_test_lang_process_pipeline) {
140+
fossil_lang_pipeline_t pipe = {0}; // Assume default enables all
141+
fossil_lang_result_t result = {0};
142+
fossil_lang_process(&pipe, "Is this a test?", &result);
143+
// Check at least one field is filled
144+
ASSUME_ITS_TRUE(result.token_count > 0 || result.is_question);
145+
}
146+
147+
// * * * * * * * * * * * * * * * * * * * * * * * *
148+
// * Fossil Logic Test Pool
149+
// * * * * * * * * * * * * * * * * * * * * * * * *
150+
FOSSIL_TEST_GROUP(c_lang_tests) {
151+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_tokenize_basic);
152+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_detect_emotion);
153+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_detect_bias_or_falsehood);
154+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_align_truth);
155+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_similarity);
156+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_summarize_and_normalize);
157+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_extract_focus);
158+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_estimate_trust);
159+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_embedding_similarity);
160+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_generate_variants);
161+
FOSSIL_TEST_ADD(c_lang_fixture, c_test_lang_process_pipeline);
162+
163+
FOSSIL_TEST_REGISTER(c_lang_fixture);
164+
} // end of tests

0 commit comments

Comments
 (0)