|
| 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