| 1 |
/* File: geometry_anchor_test.c; Copyright and License: see below */ |
| 2 |
|
| 3 |
#include "geometry_anchor_test.h" |
| 4 |
#include "geometry/geometry_anchor.h" |
| 5 |
#include "test_fixture.h" |
| 6 |
#include "test_expect.h" |
| 7 |
#include "test_environment_assert.h" |
| 8 |
#include "test_case_result.h" |
| 9 |
|
| 10 |
static test_fixture_t * set_up(); |
| 11 |
static void tear_down( test_fixture_t *fix ); |
| 12 |
static test_case_result_t test_geometry_anchor_init( test_fixture_t *fix ); |
| 13 |
static test_case_result_t test_geometry_anchor_align( test_fixture_t *fix ); |
| 14 |
static test_case_result_t test_geometry_anchor_align_biased( test_fixture_t *fix ); |
| 15 |
|
| 16 |
|
| 17 |
test_suite_t geometry_anchor_test_get_suite(void) |
| 18 |
{ |
| 19 |
test_suite_t result; |
| 20 |
test_suite_init( &result, |
| 21 |
"geometry_anchor_test_get_suite", |
| 22 |
TEST_CATEGORY_UNIT | TEST_CATEGORY_CONTINUOUS | TEST_CATEGORY_COVERAGE, |
| 23 |
&set_up, |
| 24 |
&tear_down |
| 25 |
); |
| 26 |
test_suite_add_test_case( &result, "test_geometry_anchor_init", &test_geometry_anchor_init ); |
| 27 |
test_suite_add_test_case( &result, "test_geometry_anchor_align", &test_geometry_anchor_align ); |
| 28 |
test_suite_add_test_case( &result, "test_geometry_anchor_align_biased", &test_geometry_anchor_align_biased ); |
| 29 |
return result; |
| 30 |
} |
| 31 |
|
| 32 |
static test_fixture_t * set_up() |
| 33 |
{ |
| 34 |
return NULL; |
| 35 |
} |
| 36 |
|
| 37 |
static void tear_down( test_fixture_t *fix ) |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
static test_case_result_t test_geometry_anchor_init( test_fixture_t *fix ) |
| 42 |
{ |
| 43 |
geometry_anchor_t my_anchor; |
| 44 |
geometry_anchor_init( &my_anchor, 20.0, 1.0, GEOMETRY_H_ALIGN_LEFT, GEOMETRY_V_ALIGN_BOTTOM ); |
| 45 |
|
| 46 |
geometry_anchor_t my_anchor_clone; |
| 47 |
geometry_anchor_copy( &my_anchor_clone, &my_anchor ); |
| 48 |
|
| 49 |
geometry_anchor_replace( &my_anchor, &my_anchor_clone ); |
| 50 |
|
| 51 |
TEST_EXPECT_EQUAL_FLOAT( 20.0, geometry_anchor_get_x( &my_anchor ) ); |
| 52 |
TEST_EXPECT_EQUAL_FLOAT( 1.0, geometry_anchor_get_y( &my_anchor ) ); |
| 53 |
TEST_EXPECT_EQUAL_INT( GEOMETRY_H_ALIGN_LEFT, geometry_anchor_get_x_align( &my_anchor ) ); |
| 54 |
TEST_EXPECT_EQUAL_INT( GEOMETRY_V_ALIGN_BOTTOM, geometry_anchor_get_y_align( &my_anchor ) ); |
| 55 |
const geometry_point_t *const pos = geometry_anchor_get_point_const( &my_anchor ); |
| 56 |
TEST_EXPECT_EQUAL_FLOAT( 20.0, geometry_point_get_x( pos ) ); |
| 57 |
TEST_EXPECT_EQUAL_FLOAT( 1.0, geometry_point_get_y( pos ) ); |
| 58 |
|
| 59 |
/* check that the trace function returns */ |
| 60 |
geometry_anchor_trace( &my_anchor ); |
| 61 |
|
| 62 |
geometry_anchor_destroy( &my_anchor ); |
| 63 |
return TEST_CASE_RESULT_OK; |
| 64 |
} |
| 65 |
|
| 66 |
static test_case_result_t test_geometry_anchor_align( test_fixture_t *fix ) |
| 67 |
{ |
| 68 |
geometry_anchor_t my_anchor; |
| 69 |
geometry_anchor_init( &my_anchor, 20.0, 1.0, GEOMETRY_H_ALIGN_LEFT, GEOMETRY_V_ALIGN_BOTTOM ); |
| 70 |
|
| 71 |
geometry_dimensions_t my_dim1; |
| 72 |
geometry_dimensions_init( &my_dim1, 10.0, 0.2 ); |
| 73 |
geometry_rectangle_t rect1 = geometry_anchor_align_dim( &my_anchor, &my_dim1 ); |
| 74 |
TEST_EXPECT_EQUAL_FLOAT( 25.0, geometry_rectangle_get_center_x( &rect1 ) ); |
| 75 |
TEST_EXPECT_EQUAL_FLOAT( 0.9, geometry_rectangle_get_center_y( &rect1 ) ); |
| 76 |
geometry_rectangle_destroy( &rect1 ); |
| 77 |
geometry_dimensions_destroy( &my_dim1 ); |
| 78 |
|
| 79 |
geometry_rectangle_t my_rect; |
| 80 |
geometry_rectangle_init( &my_rect, -1.0, -2.0, 10.0, 0.2 ); |
| 81 |
geometry_rectangle_t rect3 = geometry_anchor_align_rect( &my_anchor, &my_rect ); |
| 82 |
TEST_EXPECT_EQUAL_FLOAT( 25.0, geometry_rectangle_get_center_x( &rect3 ) ); |
| 83 |
TEST_EXPECT_EQUAL_FLOAT( 0.9, geometry_rectangle_get_center_y( &rect3 ) ); |
| 84 |
geometry_rectangle_destroy( &rect3 ); |
| 85 |
|
| 86 |
geometry_dimensions_t my_dim2; |
| 87 |
geometry_dimensions_init( &my_dim2, 10.0, 0.2 ); |
| 88 |
geometry_rectangle_t my_bounds; |
| 89 |
geometry_rectangle_init( &my_bounds, 19.0, 2.0, 100.0, 0.1 ); |
| 90 |
geometry_rectangle_t rect2 = geometry_anchor_align_biased_dim( &my_anchor, &my_dim2, &my_bounds ); |
| 91 |
TEST_EXPECT_EQUAL_FLOAT( 25.0, geometry_rectangle_get_center_x( &rect2 ) ); |
| 92 |
TEST_EXPECT_EQUAL_FLOAT( 0.9, geometry_rectangle_get_center_y( &rect2 ) ); |
| 93 |
geometry_rectangle_destroy( &rect2 ); |
| 94 |
geometry_rectangle_destroy( &my_bounds ); |
| 95 |
geometry_dimensions_destroy( &my_dim2 ); |
| 96 |
|
| 97 |
geometry_anchor_destroy( &my_anchor ); |
| 98 |
return TEST_CASE_RESULT_OK; |
| 99 |
} |
| 100 |
|
| 101 |
static test_case_result_t test_geometry_anchor_align_biased( test_fixture_t *fix ) |
| 102 |
{ |
| 103 |
geometry_anchor_t my_anchor; |
| 104 |
geometry_anchor_init( &my_anchor, 100.0, 10.0, GEOMETRY_H_ALIGN_CENTER, GEOMETRY_V_ALIGN_CENTER ); |
| 105 |
|
| 106 |
geometry_dimensions_t my_dim2; |
| 107 |
geometry_dimensions_init( &my_dim2, 20.0, 2.0 ); |
| 108 |
|
| 109 |
/* my_dim2 fits into my_bounds, no shift */ |
| 110 |
geometry_rectangle_t my_bounds; |
| 111 |
geometry_rectangle_init( &my_bounds, 70.0, 2.0, 200.0, 80.0 ); |
| 112 |
geometry_rectangle_t rect1 = geometry_anchor_align_biased_dim( &my_anchor, &my_dim2, &my_bounds ); |
| 113 |
TEST_EXPECT_EQUAL_FLOAT( 100.0, geometry_rectangle_get_center_x( &rect1 ) ); |
| 114 |
TEST_EXPECT_EQUAL_FLOAT( 10.0, geometry_rectangle_get_center_y( &rect1 ) ); |
| 115 |
geometry_rectangle_destroy( &rect1 ); |
| 116 |
geometry_rectangle_destroy( &my_bounds ); |
| 117 |
|
| 118 |
/* my_dim2 fits into my_bounds if shifted right-down (not fully centered) */ |
| 119 |
geometry_rectangle_init( &my_bounds, 95.0, 9.5, 200.0, 80.0 ); |
| 120 |
geometry_rectangle_t rect2 = geometry_anchor_align_biased_dim( &my_anchor, &my_dim2, &my_bounds ); |
| 121 |
TEST_EXPECT_EQUAL_FLOAT( 105.0, geometry_rectangle_get_center_x( &rect2 ) ); |
| 122 |
TEST_EXPECT_EQUAL_FLOAT( 10.5, geometry_rectangle_get_center_y( &rect2 ) ); |
| 123 |
geometry_rectangle_destroy( &rect2 ); |
| 124 |
geometry_rectangle_destroy( &my_bounds ); |
| 125 |
|
| 126 |
/* my_dim2 fits into my_bounds if shifted left-up (not fully centered) */ |
| 127 |
geometry_rectangle_init( &my_bounds, 70.0, 2.0, 35.0, 8.5 ); |
| 128 |
geometry_rectangle_t rect0 = geometry_anchor_align_biased_dim( &my_anchor, &my_dim2, &my_bounds ); |
| 129 |
TEST_EXPECT_EQUAL_FLOAT( 95.0, geometry_rectangle_get_center_x( &rect0 ) ); |
| 130 |
TEST_EXPECT_EQUAL_FLOAT( 9.5, geometry_rectangle_get_center_y( &rect0 ) ); |
| 131 |
geometry_rectangle_destroy( &rect0 ); |
| 132 |
geometry_rectangle_destroy( &my_bounds ); |
| 133 |
|
| 134 |
/* my_dim2 is left/top outside my_bounds, it is shifted and pulled back */ |
| 135 |
geometry_rectangle_init( &my_bounds, 150.0, 15.0, 200.0, 80.0 ); |
| 136 |
geometry_rectangle_t rect3 = geometry_anchor_align_biased_dim( &my_anchor, &my_dim2, &my_bounds ); |
| 137 |
TEST_EXPECT_EQUAL_FLOAT( 110.0, geometry_rectangle_get_center_x( &rect3 ) ); |
| 138 |
TEST_EXPECT_EQUAL_FLOAT( 11.0, geometry_rectangle_get_center_y( &rect3 ) ); |
| 139 |
geometry_rectangle_destroy( &rect3 ); |
| 140 |
geometry_rectangle_destroy( &my_bounds ); |
| 141 |
|
| 142 |
/* my_dim2 is right/bottom outside my_bounds, it is shifted and pulled back */ |
| 143 |
geometry_rectangle_init( &my_bounds, 50.0, 3.0, 39.0, 5.0 ); |
| 144 |
geometry_rectangle_t rect4 = geometry_anchor_align_biased_dim( &my_anchor, &my_dim2, &my_bounds ); |
| 145 |
TEST_EXPECT_EQUAL_FLOAT( 90.0, geometry_rectangle_get_center_x( &rect4 ) ); |
| 146 |
TEST_EXPECT_EQUAL_FLOAT( 9.0, geometry_rectangle_get_center_y( &rect4 ) ); |
| 147 |
geometry_rectangle_destroy( &rect4 ); |
| 148 |
geometry_rectangle_destroy( &my_bounds ); |
| 149 |
|
| 150 |
/* my_dim2 is too big for my_bounds but fits on top/left */ |
| 151 |
geometry_rectangle_init( &my_bounds, 89.0, 8.6, 3.0, 1.0 ); |
| 152 |
geometry_rectangle_t rect5 = geometry_anchor_align_biased_dim( &my_anchor, &my_dim2, &my_bounds ); |
| 153 |
TEST_EXPECT_EQUAL_FLOAT( 90.5, geometry_rectangle_get_center_x( &rect5 ) ); |
| 154 |
TEST_EXPECT_EQUAL_FLOAT( 9.1, geometry_rectangle_get_center_y( &rect5 ) ); |
| 155 |
geometry_rectangle_destroy( &rect5 ); |
| 156 |
geometry_rectangle_destroy( &my_bounds ); |
| 157 |
|
| 158 |
/* my_dim2 is too big for my_bounds but fits on bottom/right */ |
| 159 |
geometry_rectangle_init( &my_bounds, 107.0, 10.4, 3.0, 1.0 ); |
| 160 |
geometry_rectangle_t rect6 = geometry_anchor_align_biased_dim( &my_anchor, &my_dim2, &my_bounds ); |
| 161 |
TEST_EXPECT_EQUAL_FLOAT( 108.5, geometry_rectangle_get_center_x( &rect6 ) ); |
| 162 |
TEST_EXPECT_EQUAL_FLOAT( 10.9, geometry_rectangle_get_center_y( &rect6 ) ); |
| 163 |
geometry_rectangle_destroy( &rect6 ); |
| 164 |
geometry_rectangle_destroy( &my_bounds ); |
| 165 |
|
| 166 |
geometry_dimensions_destroy( &my_dim2 ); |
| 167 |
|
| 168 |
geometry_anchor_destroy( &my_anchor ); |
| 169 |
return TEST_CASE_RESULT_OK; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Copyright 2025-2026 Andreas Warnke |
| 174 |
* |
| 175 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 176 |
* you may not use this file except in compliance with the License. |
| 177 |
* You may obtain a copy of the License at |
| 178 |
* |
| 179 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 180 |
* |
| 181 |
* Unless required by applicable law or agreed to in writing, software |
| 182 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 183 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 184 |
* See the License for the specific language governing permissions and |
| 185 |
* limitations under the License. |
| 186 |
*/ |