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
|
/**
* test_glamac_view_simple.c - Simplified unit tests for view 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/glamac_view.h"
#include "../../include/glass_data.h"
// Test view initialization
int test_view_initialization() {
TEST_START("View Initialization");
ViewState view;
init_view_state(&view, 800, 600);
// Check initial values
TEST_ASSERT_FLOAT_EQ(0.0f, view.offsetX, 0.001f, "Initial X offset should be 0");
TEST_ASSERT_FLOAT_EQ(0.0f, view.offsetY, 0.001f, "Initial Y offset should be 0");
TEST_ASSERT_FLOAT_EQ(1.0f, view.zoomLevel, 0.001f, "Initial zoom should be 1.0");
TEST_ASSERT_EQ(800, view.windowWidth, "Window width should be set correctly");
TEST_ASSERT_EQ(600, view.windowHeight, "Window height should be set correctly");
TEST_END();
}
// Test coordinate transformations
int test_coordinate_transformations() {
TEST_START("Coordinate Transformations");
ViewState view;
init_view_state(&view, 800, 600);
// Initialize glass data for proper coordinate system
initialize_glass_data();
refresh_view_data_range(&view);
// Test data to screen coordinate conversion
i32 screenX, screenY;
data_to_screen_coords(60.0f, 1.5f, &view, &screenX, &screenY);
// Screen coordinates should be within reasonable bounds
TEST_ASSERT(screenX >= -100 && screenX <= view.windowWidth + 100,
"Screen X coordinate should be near window bounds");
TEST_ASSERT(screenY >= -100 && screenY <= view.windowHeight + 100,
"Screen Y coordinate should be near window bounds");
// Test screen to data coordinate conversion (inverse operation)
f32 abbeNumber, refractiveIndex;
screen_to_data_coords(screenX, screenY, &view, &abbeNumber, &refractiveIndex);
// The inverse transformation should approximately recover original values
TEST_ASSERT_FLOAT_EQ(60.0f, abbeNumber, 1.0f,
"Inverse transformation should recover Abbe number");
TEST_ASSERT_FLOAT_EQ(1.5f, refractiveIndex, 0.01f,
"Inverse transformation should recover refractive index");
cleanup_glass_data();
TEST_END();
}
// Test zoom functionality
int test_zoom_functionality() {
TEST_START("Zoom Functionality");
ViewState view;
init_view_state(&view, 800, 600);
// Test zoom in
f32 initial_zoom = view.zoomLevel;
handle_mouse_wheel_zoom(1, 400, 300, &view); // Zoom in at center
TEST_ASSERT(view.zoomLevel > initial_zoom, "Zoom should increase");
TEST_ASSERT(view.zoomLevel <= MAX_ZOOM, "Zoom should not exceed maximum");
// Test zoom out
f32 zoomed_in = view.zoomLevel;
handle_mouse_wheel_zoom(-1, 400, 300, &view); // Zoom out at center
TEST_ASSERT(view.zoomLevel < zoomed_in, "Zoom should decrease");
TEST_ASSERT(view.zoomLevel >= MIN_ZOOM, "Zoom should not go below minimum");
TEST_END();
}
// Test view reset
int test_view_reset() {
TEST_START("View Reset");
ViewState view;
init_view_state(&view, 800, 600);
// Modify the view
view.offsetX = 100.0f;
view.offsetY = 50.0f;
view.zoomLevel = 2.0f;
// Reset view
reset_view(&view);
// Check that view is back to initial state
TEST_ASSERT_FLOAT_EQ(0.0f, view.offsetX, 0.001f, "X offset should be reset to 0");
TEST_ASSERT_FLOAT_EQ(0.0f, view.offsetY, 0.001f, "Y offset should be reset to 0");
TEST_ASSERT_FLOAT_EQ(1.0f, view.zoomLevel, 0.001f, "Zoom should be reset to 1.0");
TEST_END();
}
// Test data range functionality
int test_data_range() {
TEST_START("Data Range");
ViewState view;
init_view_state(&view, 800, 600);
initialize_glass_data();
refresh_view_data_range(&view);
// After refreshing, we should have valid data ranges
TEST_ASSERT(view.minAbbe < view.maxAbbe, "Min Abbe should be less than max Abbe");
TEST_ASSERT(view.minRI < view.maxRI, "Min RI should be less than max RI");
TEST_ASSERT(view.minAbbe > 0.0f, "Min Abbe should be positive");
TEST_ASSERT(view.maxAbbe < 200.0f, "Max Abbe should be reasonable");
TEST_ASSERT(view.minRI > 1.0f, "Min RI should be greater than 1.0");
TEST_ASSERT(view.maxRI < 4.0f, "Max RI should be reasonable");
cleanup_glass_data();
TEST_END();
}
// Test visible range calculation
int test_visible_range() {
TEST_START("Visible Range Calculation");
ViewState view;
init_view_state(&view, 800, 600);
initialize_glass_data();
refresh_view_data_range(&view);
f32 visibleMinAbbe, visibleMaxAbbe, visibleMinRI, visibleMaxRI;
get_visible_data_range(&view, &visibleMinAbbe, &visibleMaxAbbe, &visibleMinRI, &visibleMaxRI);
// Visible range should be valid
TEST_ASSERT(visibleMinAbbe < visibleMaxAbbe, "Visible min Abbe should be less than max");
TEST_ASSERT(visibleMinRI < visibleMaxRI, "Visible min RI should be less than max");
cleanup_glass_data();
TEST_END();
}
// Main test runner
int main() {
printf(BLUE "=== View Management Unit Tests (Simplified) ===" RESET "\n\n");
RUN_TEST(test_view_initialization);
RUN_TEST(test_coordinate_transformations);
RUN_TEST(test_zoom_functionality);
RUN_TEST(test_view_reset);
RUN_TEST(test_data_range);
RUN_TEST(test_visible_range);
TEST_SUMMARY();
}
|