From 04b3fcb479f5aaae06d18b315a8bdc8c298f4eae Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 5 Aug 2025 11:28:41 +0200 Subject: removed clustering --- tests/unit/test_glass_data.c | 174 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 tests/unit/test_glass_data.c (limited to 'tests/unit/test_glass_data.c') diff --git a/tests/unit/test_glass_data.c b/tests/unit/test_glass_data.c new file mode 100644 index 0000000..3498747 --- /dev/null +++ b/tests/unit/test_glass_data.c @@ -0,0 +1,174 @@ +/** + * test_glass_data.c - Unit tests for glass data management + * + * Copyright (C) 2025 https://optics-design.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + */ + +#include "../test_framework.h" +#include "../../include/glass_data.h" +#include "../../include/glamac_errors.h" + +// Test glass data initialization +int test_glass_data_initialization() { + TEST_START("Glass Data Initialization"); + + initialize_glass_data(); + + // After initialization, we should have at least the default glasses + u32 count = get_glass_count(); + TEST_ASSERT(count > 0, "Should have at least some default glasses"); + + // Test that we can get the first glass + const Glass* glass = get_glass(0); + TEST_ASSERT_NOT_NULL(glass, "Should be able to get first glass"); + + // Test bounds checking - invalid index should return NULL + const Glass* invalid_glass = get_glass(999999); + TEST_ASSERT_NULL(invalid_glass, "Invalid index should return NULL"); + + TEST_END(); +} + +// Test glass data range calculation +int test_glass_data_range() { + TEST_START("Glass Data Range Calculation"); + + initialize_glass_data(); + + f32 minAbbe, maxAbbe, minRI, maxRI; + find_glass_data_range(&minAbbe, &maxAbbe, &minRI, &maxRI); + + // Validate that ranges make sense + TEST_ASSERT(minAbbe < maxAbbe, "Min Abbe should be less than max Abbe"); + TEST_ASSERT(minRI < maxRI, "Min RI should be less than max RI"); + TEST_ASSERT(minAbbe > 0.0f, "Min Abbe should be positive"); + TEST_ASSERT(maxAbbe < 200.0f, "Max Abbe should be reasonable (< 200)"); + TEST_ASSERT(minRI > 1.0f, "Min RI should be greater than 1.0"); + TEST_ASSERT(maxRI < 4.0f, "Max RI should be reasonable (< 4.0)"); + + TEST_END(); +} + +// Test JSON loading with test data +int test_json_loading() { + TEST_START("JSON Loading"); + + // Try to load test data + b32 result = load_glasses_from_json((const byte*)"tests/data/test_glasses.json", NULL); + + if (result) { + // Verify that we loaded some glasses + u32 count = get_glass_count(); + TEST_ASSERT(count > 0, "Should have loaded glasses from test JSON"); + + // Test catalog functionality + u32 catalog_count = get_catalog_count(); + TEST_ASSERT(catalog_count > 0, "Should have at least one catalog"); + + // Test catalog name retrieval + const char* catalog_name = get_catalog_name(0); + TEST_ASSERT_NOT_NULL(catalog_name, "Should be able to get catalog name"); + + // Test catalog switching + if (catalog_count > 1) { + set_current_catalog(1); + const char* second_catalog = get_current_catalog_name(); + TEST_ASSERT_NOT_NULL(second_catalog, "Should be able to switch catalogs"); + } + } else { + printf(YELLOW " Warning: Could not load test JSON file - this may be expected in some test environments" RESET "\n"); + } + + TEST_END(); +} + +// Test glass name retrieval +int test_glass_name_retrieval() { + TEST_START("Glass Name Retrieval"); + + initialize_glass_data(); + + u32 count = get_glass_count(); + if (count > 0) { + const byte* name = get_glass_name(0); + TEST_ASSERT_NOT_NULL(name, "Should be able to get glass name"); + + // Verify name is not empty + TEST_ASSERT(strlen((const char*)name) > 0, "Glass name should not be empty"); + } + + // Test invalid index + const byte* invalid_name = get_glass_name(999999); + TEST_ASSERT_NULL(invalid_name, "Invalid index should return NULL name"); + + TEST_END(); +} + +// Test catalog cycling +int test_catalog_cycling() { + TEST_START("Catalog Cycling"); + + initialize_glass_data(); + + u32 initial_catalog_count = get_catalog_count(); + + if (initial_catalog_count > 1) { + // Get initial catalog + const char* initial_name = get_current_catalog_name(); + TEST_ASSERT_NOT_NULL(initial_name, "Should have current catalog name"); + + // Cycle forward + cycle_catalog(1); + const char* next_name = get_current_catalog_name(); + TEST_ASSERT_NOT_NULL(next_name, "Should have catalog name after cycling"); + + // Cycle backward + cycle_catalog(-1); + const char* back_name = get_current_catalog_name(); + TEST_ASSERT_STR_EQ(initial_name, back_name, "Should return to initial catalog after cycling back"); + } else { + printf(YELLOW " Info: Only one catalog available - skipping cycling test" RESET "\n"); + } + + TEST_END(); +} + +// Test data validation with invalid data +int test_data_validation() { + TEST_START("Data Validation"); + + // This test would require access to internal validation functions + // For now, we'll test through the public API by trying to load invalid data + + // Try loading from non-existent file + b32 result = load_glasses_from_json((const byte*)"nonexistent_file.json", NULL); + TEST_ASSERT(!result, "Should fail to load non-existent file"); + + // Test with invalid file path (NULL) + b32 null_result = load_glasses_from_json(NULL, NULL); + TEST_ASSERT(!null_result, "Should fail with NULL path"); + + TEST_END(); +} + +// Main test runner +int main() { + printf(BLUE "=== Glass Data Unit Tests ===" RESET "\n\n"); + + RUN_TEST(test_glass_data_initialization); + RUN_TEST(test_glass_data_range); + RUN_TEST(test_json_loading); + RUN_TEST(test_glass_name_retrieval); + RUN_TEST(test_catalog_cycling); + RUN_TEST(test_data_validation); + + // Cleanup + cleanup_glass_data(); + + TEST_SUMMARY(); +} \ No newline at end of file -- cgit v1.2.3