| 1 |
// Copyright 2017 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package math |
| 15 |
|
| 16 |
import ( |
| 17 |
"math" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
) |
| 22 |
|
| 23 |
func TestBasicNSArithmetic(t *testing.T) { |
| 24 |
t.Parallel() |
| 25 |
c := qt.New(t) |
| 26 |
|
| 27 |
ns := New(nil) |
| 28 |
|
| 29 |
type TestCase struct { |
| 30 |
fn func(inputs ...any) (any, error) |
| 31 |
values []any |
| 32 |
expect any |
| 33 |
} |
| 34 |
|
| 35 |
for _, test := range []TestCase{ |
| 36 |
{ns.Add, []any{4, 2}, int64(6)}, |
| 37 |
{ns.Add, []any{4, 2, 5}, int64(11)}, |
| 38 |
{ns.Add, []any{1.0, "foo"}, false}, |
| 39 |
{ns.Add, []any{0}, false}, |
| 40 |
{ns.Sub, []any{4, 2}, int64(2)}, |
| 41 |
{ns.Sub, []any{4, 2, 5}, int64(-3)}, |
| 42 |
{ns.Sub, []any{1.0, "foo"}, false}, |
| 43 |
{ns.Sub, []any{0}, false}, |
| 44 |
{ns.Mul, []any{4, 2}, int64(8)}, |
| 45 |
{ns.Mul, []any{4, 2, 5}, int64(40)}, |
| 46 |
{ns.Mul, []any{1.0, "foo"}, false}, |
| 47 |
{ns.Mul, []any{0}, false}, |
| 48 |
{ns.Div, []any{4, 2}, int64(2)}, |
| 49 |
{ns.Div, []any{4, 2, 5}, int64(0)}, |
| 50 |
{ns.Div, []any{1.0, "foo"}, false}, |
| 51 |
{ns.Div, []any{0}, false}, |
| 52 |
} { |
| 53 |
|
| 54 |
result, err := test.fn(test.values...) |
| 55 |
|
| 56 |
if b, ok := test.expect.(bool); ok && !b { |
| 57 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 58 |
continue |
| 59 |
} |
| 60 |
|
| 61 |
c.Assert(err, qt.IsNil) |
| 62 |
c.Assert(result, qt.Equals, test.expect) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestAbs(t *testing.T) { |
| 67 |
t.Parallel() |
| 68 |
c := qt.New(t) |
| 69 |
ns := New(nil) |
| 70 |
|
| 71 |
for _, test := range []struct { |
| 72 |
x any |
| 73 |
expect any |
| 74 |
}{ |
| 75 |
{0.0, 0.0}, |
| 76 |
{1.5, 1.5}, |
| 77 |
{-1.5, 1.5}, |
| 78 |
{-2, 2.0}, |
| 79 |
{"abc", false}, |
| 80 |
} { |
| 81 |
result, err := ns.Abs(test.x) |
| 82 |
|
| 83 |
if b, ok := test.expect.(bool); ok && !b { |
| 84 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 85 |
continue |
| 86 |
} |
| 87 |
|
| 88 |
c.Assert(err, qt.IsNil) |
| 89 |
c.Assert(result, qt.Equals, test.expect) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestCeil(t *testing.T) { |
| 94 |
t.Parallel() |
| 95 |
c := qt.New(t) |
| 96 |
ns := New(nil) |
| 97 |
|
| 98 |
for _, test := range []struct { |
| 99 |
x any |
| 100 |
expect any |
| 101 |
}{ |
| 102 |
{0.1, 1.0}, |
| 103 |
{0.5, 1.0}, |
| 104 |
{1.1, 2.0}, |
| 105 |
{1.5, 2.0}, |
| 106 |
{-0.1, 0.0}, |
| 107 |
{-0.5, 0.0}, |
| 108 |
{-1.1, -1.0}, |
| 109 |
{-1.5, -1.0}, |
| 110 |
{"abc", false}, |
| 111 |
} { |
| 112 |
|
| 113 |
result, err := ns.Ceil(test.x) |
| 114 |
|
| 115 |
if b, ok := test.expect.(bool); ok && !b { |
| 116 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 117 |
continue |
| 118 |
} |
| 119 |
|
| 120 |
c.Assert(err, qt.IsNil) |
| 121 |
c.Assert(result, qt.Equals, test.expect) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func TestFloor(t *testing.T) { |
| 126 |
t.Parallel() |
| 127 |
c := qt.New(t) |
| 128 |
|
| 129 |
ns := New(nil) |
| 130 |
|
| 131 |
for _, test := range []struct { |
| 132 |
x any |
| 133 |
expect any |
| 134 |
}{ |
| 135 |
{0.1, 0.0}, |
| 136 |
{0.5, 0.0}, |
| 137 |
{1.1, 1.0}, |
| 138 |
{1.5, 1.0}, |
| 139 |
{-0.1, -1.0}, |
| 140 |
{-0.5, -1.0}, |
| 141 |
{-1.1, -2.0}, |
| 142 |
{-1.5, -2.0}, |
| 143 |
{"abc", false}, |
| 144 |
} { |
| 145 |
|
| 146 |
result, err := ns.Floor(test.x) |
| 147 |
|
| 148 |
if b, ok := test.expect.(bool); ok && !b { |
| 149 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 150 |
continue |
| 151 |
} |
| 152 |
|
| 153 |
c.Assert(err, qt.IsNil) |
| 154 |
c.Assert(result, qt.Equals, test.expect) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func TestLog(t *testing.T) { |
| 159 |
t.Parallel() |
| 160 |
c := qt.New(t) |
| 161 |
|
| 162 |
ns := New(nil) |
| 163 |
|
| 164 |
for _, test := range []struct { |
| 165 |
a any |
| 166 |
expect any |
| 167 |
}{ |
| 168 |
{1, 0.0}, |
| 169 |
{3, 1.0986}, |
| 170 |
{0, math.Inf(-1)}, |
| 171 |
{1.0, 0.0}, |
| 172 |
{3.1, 1.1314}, |
| 173 |
{"abc", false}, |
| 174 |
} { |
| 175 |
|
| 176 |
result, err := ns.Log(test.a) |
| 177 |
|
| 178 |
if b, ok := test.expect.(bool); ok && !b { |
| 179 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 180 |
continue |
| 181 |
} |
| 182 |
|
| 183 |
// we compare only 4 digits behind point if its a real float |
| 184 |
// otherwise we usually get different float values on the last positions |
| 185 |
if result != math.Inf(-1) { |
| 186 |
result = float64(int(result*10000)) / 10000 |
| 187 |
} |
| 188 |
|
| 189 |
c.Assert(err, qt.IsNil) |
| 190 |
c.Assert(result, qt.Equals, test.expect) |
| 191 |
} |
| 192 |
|
| 193 |
// Separate test for Log(-1) -- returns NaN |
| 194 |
result, err := ns.Log(-1) |
| 195 |
c.Assert(err, qt.IsNil) |
| 196 |
c.Assert(result, qt.Satisfies, math.IsNaN) |
| 197 |
} |
| 198 |
|
| 199 |
func TestSqrt(t *testing.T) { |
| 200 |
t.Parallel() |
| 201 |
c := qt.New(t) |
| 202 |
|
| 203 |
ns := New(nil) |
| 204 |
|
| 205 |
for _, test := range []struct { |
| 206 |
a any |
| 207 |
expect any |
| 208 |
}{ |
| 209 |
{81, 9.0}, |
| 210 |
{0.25, 0.5}, |
| 211 |
{0, 0.0}, |
| 212 |
{"abc", false}, |
| 213 |
} { |
| 214 |
|
| 215 |
result, err := ns.Sqrt(test.a) |
| 216 |
|
| 217 |
if b, ok := test.expect.(bool); ok && !b { |
| 218 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 219 |
continue |
| 220 |
} |
| 221 |
|
| 222 |
// we compare only 4 digits behind point if its a real float |
| 223 |
// otherwise we usually get different float values on the last positions |
| 224 |
if result != math.Inf(-1) { |
| 225 |
result = float64(int(result*10000)) / 10000 |
| 226 |
} |
| 227 |
|
| 228 |
c.Assert(err, qt.IsNil) |
| 229 |
c.Assert(result, qt.Equals, test.expect) |
| 230 |
} |
| 231 |
|
| 232 |
// Separate test for Sqrt(-1) -- returns NaN |
| 233 |
result, err := ns.Sqrt(-1) |
| 234 |
c.Assert(err, qt.IsNil) |
| 235 |
c.Assert(result, qt.Satisfies, math.IsNaN) |
| 236 |
} |
| 237 |
|
| 238 |
func TestMod(t *testing.T) { |
| 239 |
t.Parallel() |
| 240 |
c := qt.New(t) |
| 241 |
|
| 242 |
ns := New(nil) |
| 243 |
|
| 244 |
for _, test := range []struct { |
| 245 |
a any |
| 246 |
b any |
| 247 |
expect any |
| 248 |
}{ |
| 249 |
{3, 2, int64(1)}, |
| 250 |
{3, 1, int64(0)}, |
| 251 |
{3, 0, false}, |
| 252 |
{0, 3, int64(0)}, |
| 253 |
{3.1, 2, int64(1)}, |
| 254 |
{3, 2.1, int64(1)}, |
| 255 |
{3.1, 2.1, int64(1)}, |
| 256 |
{int8(3), int8(2), int64(1)}, |
| 257 |
{int16(3), int16(2), int64(1)}, |
| 258 |
{int32(3), int32(2), int64(1)}, |
| 259 |
{int64(3), int64(2), int64(1)}, |
| 260 |
{"3", "2", int64(1)}, |
| 261 |
{"3.1", "2", int64(1)}, |
| 262 |
{"aaa", "0", false}, |
| 263 |
{"3", "aaa", false}, |
| 264 |
} { |
| 265 |
|
| 266 |
result, err := ns.Mod(test.a, test.b) |
| 267 |
|
| 268 |
if b, ok := test.expect.(bool); ok && !b { |
| 269 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 270 |
continue |
| 271 |
} |
| 272 |
|
| 273 |
c.Assert(err, qt.IsNil) |
| 274 |
c.Assert(result, qt.Equals, test.expect) |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
func TestModBool(t *testing.T) { |
| 279 |
t.Parallel() |
| 280 |
c := qt.New(t) |
| 281 |
|
| 282 |
ns := New(nil) |
| 283 |
|
| 284 |
for _, test := range []struct { |
| 285 |
a any |
| 286 |
b any |
| 287 |
expect any |
| 288 |
}{ |
| 289 |
{3, 3, true}, |
| 290 |
{3, 2, false}, |
| 291 |
{3, 1, true}, |
| 292 |
{3, 0, nil}, |
| 293 |
{0, 3, true}, |
| 294 |
{3.1, 2, false}, |
| 295 |
{3, 2.1, false}, |
| 296 |
{3.1, 2.1, false}, |
| 297 |
{int8(3), int8(3), true}, |
| 298 |
{int8(3), int8(2), false}, |
| 299 |
{int16(3), int16(3), true}, |
| 300 |
{int16(3), int16(2), false}, |
| 301 |
{int32(3), int32(3), true}, |
| 302 |
{int32(3), int32(2), false}, |
| 303 |
{int64(3), int64(3), true}, |
| 304 |
{int64(3), int64(2), false}, |
| 305 |
{"3", "3", true}, |
| 306 |
{"3", "2", false}, |
| 307 |
{"3.1", "2", false}, |
| 308 |
{"aaa", "0", nil}, |
| 309 |
{"3", "aaa", nil}, |
| 310 |
} { |
| 311 |
|
| 312 |
result, err := ns.ModBool(test.a, test.b) |
| 313 |
|
| 314 |
if test.expect == nil { |
| 315 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 316 |
continue |
| 317 |
} |
| 318 |
|
| 319 |
c.Assert(err, qt.IsNil) |
| 320 |
c.Assert(result, qt.Equals, test.expect) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func TestRound(t *testing.T) { |
| 325 |
t.Parallel() |
| 326 |
c := qt.New(t) |
| 327 |
|
| 328 |
ns := New(nil) |
| 329 |
|
| 330 |
for _, test := range []struct { |
| 331 |
x any |
| 332 |
expect any |
| 333 |
}{ |
| 334 |
{0.1, 0.0}, |
| 335 |
{0.5, 1.0}, |
| 336 |
{1.1, 1.0}, |
| 337 |
{1.5, 2.0}, |
| 338 |
{-0.1, 0.0}, |
| 339 |
{-0.5, -1.0}, |
| 340 |
{-1.1, -1.0}, |
| 341 |
{-1.5, -2.0}, |
| 342 |
{"abc", false}, |
| 343 |
} { |
| 344 |
|
| 345 |
result, err := ns.Round(test.x) |
| 346 |
|
| 347 |
if b, ok := test.expect.(bool); ok && !b { |
| 348 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 349 |
continue |
| 350 |
} |
| 351 |
|
| 352 |
c.Assert(err, qt.IsNil) |
| 353 |
c.Assert(result, qt.Equals, test.expect) |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
func TestPow(t *testing.T) { |
| 358 |
t.Parallel() |
| 359 |
c := qt.New(t) |
| 360 |
|
| 361 |
ns := New(nil) |
| 362 |
|
| 363 |
for _, test := range []struct { |
| 364 |
a any |
| 365 |
b any |
| 366 |
expect any |
| 367 |
}{ |
| 368 |
{0, 0, 1.0}, |
| 369 |
{2, 0, 1.0}, |
| 370 |
{2, 3, 8.0}, |
| 371 |
{-2, 3, -8.0}, |
| 372 |
{2, -3, 0.125}, |
| 373 |
{-2, -3, -0.125}, |
| 374 |
{0.2, 3, 0.008}, |
| 375 |
{2, 0.3, 1.2311}, |
| 376 |
{0.2, 0.3, 0.617}, |
| 377 |
{"aaa", "3", false}, |
| 378 |
{"2", "aaa", false}, |
| 379 |
} { |
| 380 |
|
| 381 |
result, err := ns.Pow(test.a, test.b) |
| 382 |
|
| 383 |
if b, ok := test.expect.(bool); ok && !b { |
| 384 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 385 |
continue |
| 386 |
} |
| 387 |
|
| 388 |
// we compare only 4 digits behind point if its a real float |
| 389 |
// otherwise we usually get different float values on the last positions |
| 390 |
result = float64(int(result*10000)) / 10000 |
| 391 |
|
| 392 |
c.Assert(err, qt.IsNil) |
| 393 |
c.Assert(result, qt.Equals, test.expect) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
func TestMax(t *testing.T) { |
| 398 |
t.Parallel() |
| 399 |
c := qt.New(t) |
| 400 |
|
| 401 |
ns := New(nil) |
| 402 |
|
| 403 |
type TestCase struct { |
| 404 |
values []any |
| 405 |
expect any |
| 406 |
} |
| 407 |
|
| 408 |
for _, test := range []TestCase{ |
| 409 |
// two values |
| 410 |
{[]any{-1, -1}, -1.0}, |
| 411 |
{[]any{-1, 0}, 0.0}, |
| 412 |
{[]any{-1, 1}, 1.0}, |
| 413 |
{[]any{0, -1}, 0.0}, |
| 414 |
{[]any{0, 0}, 0.0}, |
| 415 |
{[]any{0, 1}, 1.0}, |
| 416 |
{[]any{1, -1}, 1.0}, |
| 417 |
{[]any{1, 0}, 1.0}, |
| 418 |
{[]any{32}, 32.0}, |
| 419 |
{[]any{1, 1}, 1.0}, |
| 420 |
{[]any{1.2, 1.23}, 1.23}, |
| 421 |
{[]any{-1.2, -1.23}, -1.2}, |
| 422 |
{[]any{0, "a"}, false}, |
| 423 |
{[]any{"a", 0}, false}, |
| 424 |
{[]any{"a", "b"}, false}, |
| 425 |
// Issue #11030 |
| 426 |
{[]any{7, []any{3, 4}}, 7.0}, |
| 427 |
{[]any{8, []any{3, 12}, 3}, 12.0}, |
| 428 |
{[]any{[]any{3, 5, 2}}, 5.0}, |
| 429 |
{[]any{3, []int{3, 6}, 3}, 6.0}, |
| 430 |
// No values. |
| 431 |
{[]any{}, false}, |
| 432 |
|
| 433 |
// multi values |
| 434 |
{[]any{-1, -2, -3}, -1.0}, |
| 435 |
{[]any{1, 2, 3}, 3.0}, |
| 436 |
{[]any{"a", 2, 3}, false}, |
| 437 |
} { |
| 438 |
result, err := ns.Max(test.values...) |
| 439 |
|
| 440 |
if b, ok := test.expect.(bool); ok && !b { |
| 441 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 442 |
continue |
| 443 |
} |
| 444 |
|
| 445 |
msg := qt.Commentf("values: %v", test.values) |
| 446 |
c.Assert(err, qt.IsNil, msg) |
| 447 |
c.Assert(result, qt.Equals, test.expect, msg) |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
func TestMin(t *testing.T) { |
| 452 |
t.Parallel() |
| 453 |
c := qt.New(t) |
| 454 |
|
| 455 |
ns := New(nil) |
| 456 |
|
| 457 |
type TestCase struct { |
| 458 |
values []any |
| 459 |
expect any |
| 460 |
} |
| 461 |
|
| 462 |
for _, test := range []TestCase{ |
| 463 |
// two values |
| 464 |
{[]any{-1, -1}, -1.0}, |
| 465 |
{[]any{-1, 0}, -1.0}, |
| 466 |
{[]any{-1, 1}, -1.0}, |
| 467 |
{[]any{0, -1}, -1.0}, |
| 468 |
{[]any{0, 0}, 0.0}, |
| 469 |
{[]any{0, 1}, 0.0}, |
| 470 |
{[]any{1, -1}, -1.0}, |
| 471 |
{[]any{1, 0}, 0.0}, |
| 472 |
{[]any{1, 1}, 1.0}, |
| 473 |
{[]any{2}, 2.0}, |
| 474 |
{[]any{1.2, 1.23}, 1.2}, |
| 475 |
{[]any{-1.2, -1.23}, -1.23}, |
| 476 |
{[]any{0, "a"}, false}, |
| 477 |
{[]any{"a", 0}, false}, |
| 478 |
{[]any{"a", "b"}, false}, |
| 479 |
// Issue #11030 |
| 480 |
{[]any{1, []any{3, 4}}, 1.0}, |
| 481 |
{[]any{8, []any{3, 2}, 3}, 2.0}, |
| 482 |
{[]any{[]any{3, 2, 2}}, 2.0}, |
| 483 |
{[]any{8, []int{3, 2}, 3}, 2.0}, |
| 484 |
|
| 485 |
// No values. |
| 486 |
{[]any{}, false}, |
| 487 |
|
| 488 |
// multi values |
| 489 |
{[]any{-1, -2, -3}, -3.0}, |
| 490 |
{[]any{1, 2, 3}, 1.0}, |
| 491 |
{[]any{"a", 2, 3}, false}, |
| 492 |
} { |
| 493 |
|
| 494 |
result, err := ns.Min(test.values...) |
| 495 |
|
| 496 |
if b, ok := test.expect.(bool); ok && !b { |
| 497 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 498 |
continue |
| 499 |
} |
| 500 |
|
| 501 |
c.Assert(err, qt.IsNil, qt.Commentf("values: %v", test.values)) |
| 502 |
c.Assert(result, qt.Equals, test.expect) |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
func TestSum(t *testing.T) { |
| 507 |
t.Parallel() |
| 508 |
c := qt.New(t) |
| 509 |
|
| 510 |
ns := New(nil) |
| 511 |
|
| 512 |
mustSum := func(values ...any) any { |
| 513 |
result, err := ns.Sum(values...) |
| 514 |
c.Assert(err, qt.IsNil) |
| 515 |
return result |
| 516 |
} |
| 517 |
|
| 518 |
c.Assert(mustSum(1, 2, 3), qt.Equals, 6.0) |
| 519 |
c.Assert(mustSum(1, 2, 3.0), qt.Equals, 6.0) |
| 520 |
c.Assert(mustSum(1, 2, []any{3, 4}), qt.Equals, 10.0) |
| 521 |
c.Assert(mustSum(23), qt.Equals, 23.0) |
| 522 |
c.Assert(mustSum([]any{23}), qt.Equals, 23.0) |
| 523 |
c.Assert(mustSum([]any{}), qt.Equals, 0.0) |
| 524 |
|
| 525 |
_, err := ns.Sum() |
| 526 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 527 |
} |
| 528 |
|
| 529 |
func TestProduct(t *testing.T) { |
| 530 |
t.Parallel() |
| 531 |
c := qt.New(t) |
| 532 |
|
| 533 |
ns := New(nil) |
| 534 |
|
| 535 |
mustProduct := func(values ...any) any { |
| 536 |
result, err := ns.Product(values...) |
| 537 |
c.Assert(err, qt.IsNil) |
| 538 |
return result |
| 539 |
} |
| 540 |
|
| 541 |
c.Assert(mustProduct(2, 2, 3), qt.Equals, 12.0) |
| 542 |
c.Assert(mustProduct(1, 2, 3.0), qt.Equals, 6.0) |
| 543 |
c.Assert(mustProduct(1, 2, []any{3, 4}), qt.Equals, 24.0) |
| 544 |
c.Assert(mustProduct(3.0), qt.Equals, 3.0) |
| 545 |
c.Assert(mustProduct([]string{}), qt.Equals, 0.0) |
| 546 |
|
| 547 |
_, err := ns.Product() |
| 548 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 549 |
} |
| 550 |
|
| 551 |
// Test trigonometric functions |
| 552 |
|
| 553 |
func TestPi(t *testing.T) { |
| 554 |
t.Parallel() |
| 555 |
c := qt.New(t) |
| 556 |
|
| 557 |
ns := New(nil) |
| 558 |
|
| 559 |
expect := 3.1415 |
| 560 |
result := ns.Pi() |
| 561 |
|
| 562 |
// we compare only 4 digits behind point if its a real float |
| 563 |
// otherwise we usually get different float values on the last positions |
| 564 |
result = float64(int(result*10000)) / 10000 |
| 565 |
|
| 566 |
c.Assert(result, qt.Equals, expect) |
| 567 |
} |
| 568 |
|
| 569 |
func TestSin(t *testing.T) { |
| 570 |
t.Parallel() |
| 571 |
c := qt.New(t) |
| 572 |
|
| 573 |
ns := New(nil) |
| 574 |
|
| 575 |
for _, test := range []struct { |
| 576 |
a any |
| 577 |
expect any |
| 578 |
}{ |
| 579 |
{0, 0.0}, |
| 580 |
{1, 0.8414}, |
| 581 |
{math.Pi / 2, 1.0}, |
| 582 |
{math.Pi, 0.0}, |
| 583 |
{-1.0, -0.8414}, |
| 584 |
{"abc", false}, |
| 585 |
} { |
| 586 |
|
| 587 |
result, err := ns.Sin(test.a) |
| 588 |
|
| 589 |
if b, ok := test.expect.(bool); ok && !b { |
| 590 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 591 |
continue |
| 592 |
} |
| 593 |
|
| 594 |
// we compare only 4 digits behind point if its a real float |
| 595 |
// otherwise we usually get different float values on the last positions |
| 596 |
result = float64(int(result*10000)) / 10000 |
| 597 |
|
| 598 |
c.Assert(err, qt.IsNil) |
| 599 |
c.Assert(result, qt.Equals, test.expect) |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
func TestCos(t *testing.T) { |
| 604 |
t.Parallel() |
| 605 |
c := qt.New(t) |
| 606 |
|
| 607 |
ns := New(nil) |
| 608 |
|
| 609 |
for _, test := range []struct { |
| 610 |
a any |
| 611 |
expect any |
| 612 |
}{ |
| 613 |
{0, 1.0}, |
| 614 |
{1, 0.5403}, |
| 615 |
{math.Pi / 2, 0.0}, |
| 616 |
{math.Pi, -1.0}, |
| 617 |
{-1.0, 0.5403}, |
| 618 |
{"abc", false}, |
| 619 |
} { |
| 620 |
|
| 621 |
result, err := ns.Cos(test.a) |
| 622 |
|
| 623 |
if b, ok := test.expect.(bool); ok && !b { |
| 624 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 625 |
continue |
| 626 |
} |
| 627 |
|
| 628 |
// we compare only 4 digits behind point if its a real float |
| 629 |
// otherwise we usually get different float values on the last positions |
| 630 |
result = float64(int(result*10000)) / 10000 |
| 631 |
|
| 632 |
c.Assert(err, qt.IsNil) |
| 633 |
c.Assert(result, qt.Equals, test.expect) |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
func TestTan(t *testing.T) { |
| 638 |
t.Parallel() |
| 639 |
c := qt.New(t) |
| 640 |
|
| 641 |
ns := New(nil) |
| 642 |
|
| 643 |
for _, test := range []struct { |
| 644 |
a any |
| 645 |
expect any |
| 646 |
}{ |
| 647 |
{0, 0.0}, |
| 648 |
{1, 1.5574}, |
| 649 |
// {math.Pi / 2, math.Inf(1)}, |
| 650 |
{math.Pi, 0.0}, |
| 651 |
{-1.0, -1.5574}, |
| 652 |
{"abc", false}, |
| 653 |
} { |
| 654 |
|
| 655 |
result, err := ns.Tan(test.a) |
| 656 |
|
| 657 |
if b, ok := test.expect.(bool); ok && !b { |
| 658 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 659 |
continue |
| 660 |
} |
| 661 |
|
| 662 |
// we compare only 4 digits behind point if its a real float |
| 663 |
// otherwise we usually get different float values on the last positions |
| 664 |
if result != math.Inf(1) { |
| 665 |
result = float64(int(result*10000)) / 10000 |
| 666 |
} |
| 667 |
|
| 668 |
c.Assert(err, qt.IsNil) |
| 669 |
c.Assert(result, qt.Equals, test.expect) |
| 670 |
} |
| 671 |
|
| 672 |
// Separate test for Tan(oo) -- returns NaN |
| 673 |
result, err := ns.Tan(math.Inf(1)) |
| 674 |
c.Assert(err, qt.IsNil) |
| 675 |
c.Assert(result, qt.Satisfies, math.IsNaN) |
| 676 |
} |
| 677 |
|
| 678 |
// Test inverse trigonometric functions |
| 679 |
|
| 680 |
func TestAsin(t *testing.T) { |
| 681 |
t.Parallel() |
| 682 |
c := qt.New(t) |
| 683 |
ns := New(nil) |
| 684 |
|
| 685 |
for _, test := range []struct { |
| 686 |
x any |
| 687 |
expect any |
| 688 |
}{ |
| 689 |
{0.0, 0.0}, |
| 690 |
{1.0, 1.5707}, |
| 691 |
{-1.0, -1.5707}, |
| 692 |
{0.5, 0.5235}, |
| 693 |
{"abc", false}, |
| 694 |
} { |
| 695 |
result, err := ns.Asin(test.x) |
| 696 |
|
| 697 |
if b, ok := test.expect.(bool); ok && !b { |
| 698 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 699 |
continue |
| 700 |
} |
| 701 |
// we compare only 4 digits behind point if its a real float |
| 702 |
// otherwise we usually get different float values on the last positions |
| 703 |
result = float64(int(result*10000)) / 10000 |
| 704 |
|
| 705 |
c.Assert(err, qt.IsNil) |
| 706 |
c.Assert(result, qt.Equals, test.expect) |
| 707 |
} |
| 708 |
|
| 709 |
// Separate test for Asin(2) -- returns NaN |
| 710 |
result, err := ns.Asin(2) |
| 711 |
c.Assert(err, qt.IsNil) |
| 712 |
c.Assert(result, qt.Satisfies, math.IsNaN) |
| 713 |
} |
| 714 |
|
| 715 |
func TestAcos(t *testing.T) { |
| 716 |
t.Parallel() |
| 717 |
c := qt.New(t) |
| 718 |
ns := New(nil) |
| 719 |
|
| 720 |
for _, test := range []struct { |
| 721 |
x any |
| 722 |
expect any |
| 723 |
}{ |
| 724 |
{1.0, 0.0}, |
| 725 |
{0.0, 1.5707}, |
| 726 |
{-1.0, 3.1415}, |
| 727 |
{0.5, 1.0471}, |
| 728 |
{"abc", false}, |
| 729 |
} { |
| 730 |
result, err := ns.Acos(test.x) |
| 731 |
|
| 732 |
if b, ok := test.expect.(bool); ok && !b { |
| 733 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 734 |
continue |
| 735 |
} |
| 736 |
|
| 737 |
// we compare only 4 digits behind point if its a real float |
| 738 |
// otherwise we usually get different float values on the last positions |
| 739 |
result = float64(int(result*10000)) / 10000 |
| 740 |
|
| 741 |
c.Assert(err, qt.IsNil) |
| 742 |
c.Assert(result, qt.Equals, test.expect) |
| 743 |
} |
| 744 |
|
| 745 |
// Separate test for Acos(2) -- returns NaN |
| 746 |
result, err := ns.Acos(2) |
| 747 |
c.Assert(err, qt.IsNil) |
| 748 |
c.Assert(result, qt.Satisfies, math.IsNaN) |
| 749 |
} |
| 750 |
|
| 751 |
func TestAtan(t *testing.T) { |
| 752 |
t.Parallel() |
| 753 |
c := qt.New(t) |
| 754 |
ns := New(nil) |
| 755 |
|
| 756 |
for _, test := range []struct { |
| 757 |
x any |
| 758 |
expect any |
| 759 |
}{ |
| 760 |
{0.0, 0.0}, |
| 761 |
{1, 0.7853}, |
| 762 |
{-1.0, -0.7853}, |
| 763 |
{math.Inf(1), 1.5707}, |
| 764 |
{"abc", false}, |
| 765 |
} { |
| 766 |
result, err := ns.Atan(test.x) |
| 767 |
|
| 768 |
if b, ok := test.expect.(bool); ok && !b { |
| 769 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 770 |
continue |
| 771 |
} |
| 772 |
|
| 773 |
// we compare only 4 digits behind point if its a real float |
| 774 |
// otherwise we usually get different float values on the last positions |
| 775 |
result = float64(int(result*10000)) / 10000 |
| 776 |
|
| 777 |
c.Assert(err, qt.IsNil) |
| 778 |
c.Assert(result, qt.Equals, test.expect) |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
func TestAtan2(t *testing.T) { |
| 783 |
t.Parallel() |
| 784 |
c := qt.New(t) |
| 785 |
ns := New(nil) |
| 786 |
|
| 787 |
for _, test := range []struct { |
| 788 |
x any |
| 789 |
y any |
| 790 |
expect any |
| 791 |
}{ |
| 792 |
{1.0, 1.0, 0.7853}, |
| 793 |
{-1.0, 1.0, -0.7853}, |
| 794 |
{1.0, -1.0, 2.3561}, |
| 795 |
{-1.0, -1.0, -2.3561}, |
| 796 |
{1, 0, 1.5707}, |
| 797 |
{-1, 0, -1.5707}, |
| 798 |
{0, 1, 0.0}, |
| 799 |
{0, -1, 3.1415}, |
| 800 |
{0.0, 0.0, 0.0}, |
| 801 |
{"abc", "def", false}, |
| 802 |
} { |
| 803 |
result, err := ns.Atan2(test.x, test.y) |
| 804 |
|
| 805 |
if b, ok := test.expect.(bool); ok && !b { |
| 806 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 807 |
continue |
| 808 |
} |
| 809 |
|
| 810 |
// we compare only 4 digits behind point if its a real float |
| 811 |
// otherwise we usually get different float values on the last positions |
| 812 |
result = float64(int(result*10000)) / 10000 |
| 813 |
|
| 814 |
c.Assert(err, qt.IsNil) |
| 815 |
c.Assert(result, qt.Equals, test.expect) |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
// Test angle helper functions |
| 820 |
|
| 821 |
func TestToDegrees(t *testing.T) { |
| 822 |
t.Parallel() |
| 823 |
c := qt.New(t) |
| 824 |
ns := New(nil) |
| 825 |
|
| 826 |
for _, test := range []struct { |
| 827 |
x any |
| 828 |
expect any |
| 829 |
}{ |
| 830 |
{0.0, 0.0}, |
| 831 |
{1, 57.2957}, |
| 832 |
{math.Pi / 2, 90.0}, |
| 833 |
{math.Pi, 180.0}, |
| 834 |
{"abc", false}, |
| 835 |
} { |
| 836 |
result, err := ns.ToDegrees(test.x) |
| 837 |
|
| 838 |
if b, ok := test.expect.(bool); ok && !b { |
| 839 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 840 |
continue |
| 841 |
} |
| 842 |
|
| 843 |
// we compare only 4 digits behind point if its a real float |
| 844 |
// otherwise we usually get different float values on the last positions |
| 845 |
result = float64(int(result*10000)) / 10000 |
| 846 |
|
| 847 |
c.Assert(err, qt.IsNil) |
| 848 |
c.Assert(result, qt.Equals, test.expect) |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
func TestToRadians(t *testing.T) { |
| 853 |
t.Parallel() |
| 854 |
c := qt.New(t) |
| 855 |
ns := New(nil) |
| 856 |
|
| 857 |
for _, test := range []struct { |
| 858 |
x any |
| 859 |
expect any |
| 860 |
}{ |
| 861 |
{0, 0.0}, |
| 862 |
{57.29577951308232, 1.0}, |
| 863 |
{90, 1.5707}, |
| 864 |
{180.0, 3.1415}, |
| 865 |
{"abc", false}, |
| 866 |
} { |
| 867 |
result, err := ns.ToRadians(test.x) |
| 868 |
|
| 869 |
if b, ok := test.expect.(bool); ok && !b { |
| 870 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 871 |
continue |
| 872 |
} |
| 873 |
|
| 874 |
// we compare only 4 digits behind point if its a real float |
| 875 |
// otherwise we usually get different float values on the last positions |
| 876 |
result = float64(int(result*10000)) / 10000 |
| 877 |
|
| 878 |
c.Assert(err, qt.IsNil) |
| 879 |
c.Assert(result, qt.Equals, test.expect) |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
func TestMaxInt64(t *testing.T) { |
| 884 |
t.Parallel() |
| 885 |
ns := New(nil) |
| 886 |
|
| 887 |
var want int64 = 9223372036854775807 |
| 888 |
got := ns.MaxInt64() |
| 889 |
if want != got { |
| 890 |
t.Errorf("want %d, got %d", want, got) |
| 891 |
} |
| 892 |
} |