/** * 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(); }