1 module doml.tests.math_test; 2 3 import Math = doml.math; 4 import std.stdio; 5 import doml.rounding_mode; 6 import doml.tests.dunit_tests; 7 8 import doml.vector_2d; 9 import doml.vector_3d; 10 import doml.vector_4d; 11 12 import doml.rounding_mode; 13 14 /* 15 * The MIT License 16 * 17 * Copyright (c) 2018-2022 JOML. 18 @#$%@$% Translated by jordan4ibanez 19 * 20 * Permission is hereby granted, free of charge, to any person obtaining a copy 21 * of this software and associated documentation files (the "Software"), to deal 22 * in the Software without restriction, including without limitation the rights 23 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 24 * copies of the Software, and to permit persons to whom the Software is 25 * furnished to do so, subject to the following conditions: 26 * 27 * The above copyright notice and this permission notice shall be included in 28 * all copies or substantial portions of the Software. 29 * 30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 33 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 36 * THE SOFTWARE. 37 */ 38 39 /** 40 * Tests for Math.vecLength - addressing <a href="https://github.com/JOML-CI/JOML/issues/131">Issue #131</a> 41 * 42 * @author F. Neurath 43 */ 44 45 unittest { 46 writeln("\nBEGIN MATH LIBRARY\n"); 47 48 writeln("BEGINNING CLAMP TEST"); 49 // Integer value tests 50 assert(Math.clamp(10,20,0) == 10); 51 assert(Math.clamp(10,20,12) == 12); 52 assert(Math.clamp(10,20,30) == 20); 53 54 // double value tests 55 assertEquals(Math.clamp(10.0,20.0,0.0),10.0, 1000); // .0001f 10000 56 assertEquals(Math.clamp(10.0,20.0,12.0),12.0, 1000); // .0001f 10000 57 assertEquals(Math.clamp(10.0,20.0,30.0),20.0, 1000); // .0001f 10000 58 writeln("PASSED!"); 59 60 61 writeln("BEGINNING VECTOR LENGTH TEST"); 62 // Integer value tests 63 assertEquals(5., Vector2d.length(4, 3), 1000); 64 assertEquals(6., Vector3d.length(2, -4, 4), 1000); 65 assertEquals(3., Vector4d.length(2, -1, 0, -2), 1000); 66 67 // doubleing point value tests 68 assertEquals(Math.sqrt(.41), Vector2d.length(.4, -.5), 1000); 69 assertEquals(Math.sqrt(.3), Vector3d.length(.1, -.5, .2), 1000); 70 assertEquals(1., Vector4d.length(.5, .5, .5, .5), 1000); 71 72 writeln("PASSED!"); 73 writeln("BEGINNING VECTOR INFINITE TEST"); 74 75 // double point value tests 76 assertFalse(Math.isFinite(double.nan)); 77 assertTrue(Math.isFinite(1.0)); 78 assertTrue(Math.isFinite(-1.0)); 79 assertFalse(Math.isFinite(double.infinity)); 80 assertFalse(Math.isFinite(-double.infinity)); 81 82 writeln("PASSED!"); 83 writeln("BEGINNING ROUNDING TEST"); 84 85 // TRUNCATE 86 assertEquals(0, Math.roundUsing(0.2, RoundingMode.TRUNCATE)); 87 assertEquals(0, Math.roundUsing(0.5, RoundingMode.TRUNCATE)); 88 assertEquals(0, Math.roundUsing(0.9, RoundingMode.TRUNCATE)); 89 assertEquals(1, Math.roundUsing(1.0, RoundingMode.TRUNCATE)); 90 assertEquals(0, Math.roundUsing(-0.2, RoundingMode.TRUNCATE)); 91 assertEquals(0, Math.roundUsing(-0.5, RoundingMode.TRUNCATE)); 92 assertEquals(0, Math.roundUsing(-0.9, RoundingMode.TRUNCATE)); 93 assertEquals(-1, Math.roundUsing(-1.0, RoundingMode.TRUNCATE)); 94 // CEILING 95 assertEquals(1, Math.roundUsing(0.2, RoundingMode.CEILING)); 96 assertEquals(1, Math.roundUsing(0.5, RoundingMode.CEILING)); 97 assertEquals(1, Math.roundUsing(0.9, RoundingMode.CEILING)); 98 assertEquals(1, Math.roundUsing(1.0, RoundingMode.CEILING)); 99 assertEquals(0, Math.roundUsing(-0.2, RoundingMode.CEILING)); 100 assertEquals(0, Math.roundUsing(-0.5, RoundingMode.CEILING)); 101 assertEquals(0, Math.roundUsing(-0.9, RoundingMode.CEILING)); 102 assertEquals(-1, Math.roundUsing(-1.0, RoundingMode.CEILING)); 103 // FLOOR 104 assertEquals(0, Math.roundUsing(0.2, RoundingMode.FLOOR)); 105 assertEquals(0, Math.roundUsing(0.5, RoundingMode.FLOOR)); 106 assertEquals(0, Math.roundUsing(0.9, RoundingMode.FLOOR)); 107 assertEquals(1, Math.roundUsing(1.0, RoundingMode.FLOOR)); 108 assertEquals(-1, Math.roundUsing(-0.2, RoundingMode.FLOOR)); 109 assertEquals(-1, Math.roundUsing(-0.5, RoundingMode.FLOOR)); 110 assertEquals(-1, Math.roundUsing(-0.9, RoundingMode.FLOOR)); 111 assertEquals(-1, Math.roundUsing(-1.0, RoundingMode.FLOOR)); 112 // HALF_DOWN 113 assertEquals(0, Math.roundUsing(0.2, RoundingMode.HALF_DOWN)); 114 assertEquals(0, Math.roundUsing(0.5, RoundingMode.HALF_DOWN)); 115 assertEquals(1, Math.roundUsing(0.9, RoundingMode.HALF_DOWN)); 116 assertEquals(1, Math.roundUsing(1.0, RoundingMode.HALF_DOWN)); 117 assertEquals(0, Math.roundUsing(-0.2, RoundingMode.HALF_DOWN)); 118 assertEquals(0, Math.roundUsing(-0.5, RoundingMode.HALF_DOWN)); 119 assertEquals(-1, Math.roundUsing(-0.9, RoundingMode.HALF_DOWN)); 120 assertEquals(-1, Math.roundUsing(-1.0, RoundingMode.HALF_DOWN)); 121 // HALF_UP 122 assertEquals(0, Math.roundUsing(0.2, RoundingMode.HALF_UP)); 123 assertEquals(1, Math.roundUsing(0.5, RoundingMode.HALF_UP)); 124 assertEquals(1, Math.roundUsing(0.9, RoundingMode.HALF_UP)); 125 assertEquals(1, Math.roundUsing(1.0, RoundingMode.HALF_UP)); 126 assertEquals(0, Math.roundUsing(-0.2, RoundingMode.HALF_UP)); 127 assertEquals(-1, Math.roundUsing(-0.5, RoundingMode.HALF_UP)); 128 assertEquals(-1, Math.roundUsing(-0.9, RoundingMode.HALF_UP)); 129 assertEquals(-1, Math.roundUsing(-1.0, RoundingMode.HALF_UP)); 130 // HALF_EVEN 131 assertEquals(0, Math.roundUsing(0.2, RoundingMode.HALF_EVEN)); 132 assertEquals(0, Math.roundUsing(0.5, RoundingMode.HALF_EVEN)); 133 assertEquals(1, Math.roundUsing(0.9, RoundingMode.HALF_EVEN)); 134 assertEquals(1, Math.roundUsing(1.0, RoundingMode.HALF_EVEN)); 135 assertEquals(0, Math.roundUsing(-0.2, RoundingMode.HALF_EVEN)); 136 assertEquals(0, Math.roundUsing(-0.5, RoundingMode.HALF_EVEN)); 137 assertEquals(-1, Math.roundUsing(-0.9, RoundingMode.HALF_EVEN)); 138 assertEquals(-1, Math.roundUsing(-1.0, RoundingMode.HALF_EVEN)); 139 writeln("PASSED!"); 140 }