| 1 |
/* File: geometry_non_linear_scale_test.c; Copyright and License: see below */ |
| 2 |
|
| 3 |
#include "geometry_non_linear_scale_test.h" |
| 4 |
#include "geometry/geometry_non_linear_scale.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_init( test_fixture_t *fix ); |
| 13 |
static test_case_result_t test_get_grid( test_fixture_t *fix ); |
| 14 |
static test_case_result_t test_scale_conversion( test_fixture_t *fix ); |
| 15 |
static test_case_result_t test_buffer_exceeded( test_fixture_t *fix ); |
| 16 |
|
| 17 |
test_suite_t geometry_non_linear_scale_test_get_suite(void) |
| 18 |
{ |
| 19 |
test_suite_t result; |
| 20 |
test_suite_init( &result, |
| 21 |
"geometry_non_linear_scale_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_init", &test_init ); |
| 27 |
test_suite_add_test_case( &result, "test_get_grid", &test_get_grid ); |
| 28 |
test_suite_add_test_case( &result, "test_scale_conversion", &test_scale_conversion ); |
| 29 |
test_suite_add_test_case( &result, "test_buffer_exceeded", &test_buffer_exceeded ); |
| 30 |
return result; |
| 31 |
} |
| 32 |
|
| 33 |
static test_fixture_t * set_up() |
| 34 |
{ |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
|
| 38 |
static void tear_down( test_fixture_t *fix ) |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
static test_case_result_t test_init( test_fixture_t *fix ) |
| 43 |
{ |
| 44 |
geometry_non_linear_scale_t my_scale; |
| 45 |
|
| 46 |
geometry_non_linear_scale_init ( &my_scale, 0.0, 1000.0 ); |
| 47 |
const double one_quarter = geometry_non_linear_scale_get_location( &my_scale, INT32_MIN/2 ); |
| 48 |
TEST_EXPECT_EQUAL_FLOAT( 250.0, one_quarter ); |
| 49 |
|
| 50 |
geometry_non_linear_scale_reinit ( &my_scale, -250.0, 0.0 ); |
| 51 |
const double low_side = geometry_non_linear_scale_get_location( &my_scale, INT32_MIN ); |
| 52 |
TEST_EXPECT_EQUAL_FLOAT( -250.0, low_side ); |
| 53 |
|
| 54 |
geometry_non_linear_scale_destroy ( &my_scale ); |
| 55 |
return TEST_CASE_RESULT_OK; |
| 56 |
} |
| 57 |
|
| 58 |
static test_case_result_t test_get_grid( test_fixture_t *fix ) |
| 59 |
{ |
| 60 |
geometry_non_linear_scale_t my_scale; |
| 61 |
|
| 62 |
geometry_non_linear_scale_init ( &my_scale, -250.0, 0.0 ); |
| 63 |
const u8_error_t err = geometry_non_linear_scale_add_order( &my_scale, 512 ); |
| 64 |
TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, err ); |
| 65 |
|
| 66 |
const double one_quarter = geometry_non_linear_scale_get_location( &my_scale, INT32_MIN ); |
| 67 |
TEST_EXPECT_EQUAL_FLOAT( -250.0, one_quarter ); |
| 68 |
|
| 69 |
const uint32_t num = geometry_non_linear_scale_get_grid_intervals( &my_scale ); |
| 70 |
TEST_EXPECT_EQUAL_INT( 2, num ); |
| 71 |
|
| 72 |
const double dist = geometry_non_linear_scale_get_grid_distances( &my_scale ); |
| 73 |
TEST_EXPECT_EQUAL_FLOAT( 125.0, dist ); |
| 74 |
|
| 75 |
const bool on_grid_500 = geometry_non_linear_scale_is_order_on_grid( &my_scale, 500 ); |
| 76 |
TEST_EXPECT_EQUAL_INT( false, on_grid_500 ); |
| 77 |
|
| 78 |
const bool on_grid_512 = geometry_non_linear_scale_is_order_on_grid( &my_scale, 512 ); |
| 79 |
TEST_EXPECT_EQUAL_INT( true, on_grid_512 ); |
| 80 |
|
| 81 |
const bool on_grid_min = geometry_non_linear_scale_is_order_on_grid( &my_scale, INT32_MIN ); |
| 82 |
TEST_EXPECT_EQUAL_INT( false, on_grid_min ); |
| 83 |
|
| 84 |
const bool on_grid_max = geometry_non_linear_scale_is_order_on_grid( &my_scale, INT32_MAX ); |
| 85 |
TEST_EXPECT_EQUAL_INT( false, on_grid_max ); |
| 86 |
|
| 87 |
geometry_non_linear_scale_destroy ( &my_scale ); |
| 88 |
return TEST_CASE_RESULT_OK; |
| 89 |
} |
| 90 |
|
| 91 |
static test_case_result_t test_scale_conversion( test_fixture_t *fix ) |
| 92 |
{ |
| 93 |
geometry_non_linear_scale_t my_scale; |
| 94 |
double location; |
| 95 |
int32_t order; |
| 96 |
|
| 97 |
/* init and trace */ |
| 98 |
|
| 99 |
geometry_non_linear_scale_init ( &my_scale, -3.14, 3.14 ); |
| 100 |
|
| 101 |
geometry_non_linear_scale_add_order( &my_scale, -100 ); |
| 102 |
|
| 103 |
geometry_non_linear_scale_add_order( &my_scale, 200 ); |
| 104 |
|
| 105 |
geometry_non_linear_scale_add_order( &my_scale, 200 ); |
| 106 |
|
| 107 |
const uint32_t num = geometry_non_linear_scale_get_grid_intervals( &my_scale ); |
| 108 |
TEST_EXPECT_EQUAL_INT( 3, num ); |
| 109 |
|
| 110 |
geometry_non_linear_scale_trace ( &my_scale ); |
| 111 |
|
| 112 |
/* test get_location */ |
| 113 |
|
| 114 |
location = geometry_non_linear_scale_get_location( &my_scale, +200 ); |
| 115 |
TEST_EXPECT( ( +1.0 < location ) && ( location < +1.1 ) ); |
| 116 |
|
| 117 |
location = geometry_non_linear_scale_get_location( &my_scale, +100 ); |
| 118 |
TEST_EXPECT( ( +0.3 < location ) && ( location < +0.4 ) ); |
| 119 |
|
| 120 |
/* test get_order with snap */ |
| 121 |
|
| 122 |
order = geometry_non_linear_scale_get_order ( &my_scale, -1.0, 0.1 ); |
| 123 |
TEST_EXPECT_EQUAL_INT( -100, order ); |
| 124 |
|
| 125 |
/* test get_order without snap */ |
| 126 |
|
| 127 |
order = geometry_non_linear_scale_get_order ( &my_scale, -3.1, 0.1 ); |
| 128 |
TEST_EXPECT( ( INT32_MIN < order ) && ( order < -1000000000 ) ); |
| 129 |
|
| 130 |
order = geometry_non_linear_scale_get_order ( &my_scale, 3.1, 0.1 ); |
| 131 |
TEST_EXPECT( ( 1000000000 < order ) && ( order < INT32_MAX ) ); |
| 132 |
|
| 133 |
order = geometry_non_linear_scale_get_order ( &my_scale, -0.9, 0.1 ); |
| 134 |
TEST_EXPECT( ( -100 < order ) && ( order < -70 ) ); |
| 135 |
|
| 136 |
/* test get_order out of bounds */ |
| 137 |
|
| 138 |
order = geometry_non_linear_scale_get_order ( &my_scale, -3.2, 0.1 ); |
| 139 |
TEST_EXPECT_EQUAL_INT( INT32_MIN, order ); |
| 140 |
|
| 141 |
order = geometry_non_linear_scale_get_order ( &my_scale, 3.2, 0.1 ); |
| 142 |
TEST_EXPECT_EQUAL_INT( INT32_MAX, order ); |
| 143 |
|
| 144 |
geometry_non_linear_scale_destroy ( &my_scale ); |
| 145 |
return TEST_CASE_RESULT_OK; |
| 146 |
} |
| 147 |
|
| 148 |
static test_case_result_t test_buffer_exceeded( test_fixture_t *fix ) |
| 149 |
{ |
| 150 |
geometry_non_linear_scale_t my_scale; |
| 151 |
geometry_non_linear_scale_init ( &my_scale, 0.0, 1000.0 * ( GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS - 1 ) ); |
| 152 |
|
| 153 |
for ( int32_t step = 0; step < ( GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS - 2 ); step ++ ) |
| 154 |
{ |
| 155 |
const u8_error_t err0 = geometry_non_linear_scale_add_order( &my_scale, step + 1 ); |
| 156 |
TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, err0 ); |
| 157 |
} |
| 158 |
const u8_error_t err1 = geometry_non_linear_scale_add_order( &my_scale, GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS - 1 ); |
| 159 |
TEST_EXPECT_EQUAL_INT( U8_ERROR_ARRAY_BUFFER_EXCEEDED, err1 ); |
| 160 |
|
| 161 |
const bool on_grid = geometry_non_linear_scale_is_order_on_grid( &my_scale, GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS - 2 ); |
| 162 |
TEST_EXPECT_EQUAL_INT( true, on_grid ); |
| 163 |
const bool not_on_grid = geometry_non_linear_scale_is_order_on_grid( &my_scale, GEOMETRY_NON_LINEAR_SCALE_MAX_POINTS - 1 ); |
| 164 |
TEST_EXPECT_EQUAL_INT( false, not_on_grid ); |
| 165 |
|
| 166 |
geometry_non_linear_scale_destroy ( &my_scale ); |
| 167 |
return TEST_CASE_RESULT_OK; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/* |
| 172 |
* Copyright 2016-2026 Andreas Warnke |
| 173 |
* |
| 174 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 175 |
* you may not use this file except in compliance with the License. |
| 176 |
* You may obtain a copy of the License at |
| 177 |
* |
| 178 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 179 |
* |
| 180 |
* Unless required by applicable law or agreed to in writing, software |
| 181 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 182 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 183 |
* See the License for the specific language governing permissions and |
| 184 |
* limitations under the License. |
| 185 |
*/ |