test_pow.odin 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package test_internal_math_pow
  2. import "core:fmt"
  3. import "core:math"
  4. import "core:os"
  5. import "core:testing"
  6. @test
  7. pow_test :: proc(t: ^testing.T) {
  8. for exp in -2000..=2000 {
  9. {
  10. v1 := math.pow(2, f64(exp))
  11. v2 := math.pow2_f64(exp)
  12. _v1 := transmute(u64)v1
  13. _v2 := transmute(u64)v2
  14. if exp == -1075 && ODIN_OS == .Windows {
  15. // LLVM on Windows returns 0h00000000_00000001 for pow(2, -1075),
  16. // unlike macOS and Linux where it returns 0h00000000_00000000
  17. // pow2_f64 returns the same float on all platforms because it isn't this stupid
  18. _v1 = 0h00000000_00000000
  19. }
  20. expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f64(%d) == math.pow(2, %d) (= %16x), got %16x", exp, exp, _v1, _v2))
  21. }
  22. {
  23. v1 := math.pow(2, f32(exp))
  24. v2 := math.pow2_f32(exp)
  25. _v1 := transmute(u32)v1
  26. _v2 := transmute(u32)v2
  27. expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f32(%d) == math.pow(2, %d) (= %08x), got %08x", exp, exp, _v1, _v2))
  28. }
  29. {
  30. v1 := math.pow(2, f16(exp))
  31. v2 := math.pow2_f16(exp)
  32. _v2 := transmute(u16)v2
  33. _v1 := transmute(u16)v1
  34. when ODIN_OS == .Darwin && ODIN_ARCH == .arm64 {
  35. if exp == -25 {
  36. testing.logf(t, "skipping known test failure on darwin+arm64, Expected math.pow2_f16(-25) == math.pow(2, -25) (= 0000), got 0001")
  37. _v2 = 0
  38. }
  39. }
  40. expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f16(%d) == math.pow(2, %d) (= %04x), got %04x", exp, exp, _v1, _v2))
  41. }
  42. }
  43. }
  44. // -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  45. main :: proc() {
  46. t := testing.T{}
  47. pow_test(&t)
  48. fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
  49. if TEST_fail > 0 {
  50. os.exit(1)
  51. }
  52. }
  53. TEST_count := 0
  54. TEST_fail := 0
  55. when ODIN_TEST {
  56. expect :: testing.expect
  57. log :: testing.log
  58. } else {
  59. expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
  60. TEST_count += 1
  61. if !condition {
  62. TEST_fail += 1
  63. fmt.printf("[%v] %v\n", loc, message)
  64. return
  65. }
  66. }
  67. log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
  68. fmt.printf("[%v] ", loc)
  69. fmt.printf("log: %v\n", v)
  70. }
  71. }