summaryrefslogtreecommitdiff
path: root/tests/unit/test_glass_data.c
blob: 3498747eac6efb3156cb7681ad3a371cfd858bbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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();
}
Back to https://optics-design.com