test_string_compare.odin 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package test_internal_string_compare
  2. import "core:fmt"
  3. import "core:os"
  4. import "core:testing"
  5. Op :: enum { Eq, Lt, Gt }
  6. Test :: struct {
  7. a: cstring,
  8. b: cstring,
  9. res: [Op]bool,
  10. }
  11. CASES := []Test{
  12. {"hellope", "hellope", {.Eq=true, .Lt=false, .Gt=false}},
  13. {"Hellope", "hellope", {.Eq=false, .Lt=true, .Gt=false}}, // H < h
  14. {"Hell", "Hellope", {.Eq=false, .Lt=true, .Gt=false}},
  15. {"Hellope!", "Hellope", {.Eq=false, .Lt=false, .Gt=true }},
  16. {"Hellopf", "Hellope", {.Eq=false, .Lt=false, .Gt=true }},
  17. }
  18. @test
  19. string_compare :: proc(t: ^testing.T) {
  20. for v in CASES {
  21. s_a := string(v.a)
  22. s_b := string(v.b)
  23. for res, op in v.res {
  24. switch op {
  25. case .Eq:
  26. expect(t, (v.a == v.b) == res, fmt.tprintf("Expected cstring(\"%v\") == cstring(\"%v\") to be %v", v.a, v.b, res))
  27. expect(t, (s_a == s_b) == res, fmt.tprintf("Expected string(\"%v\") == string(\"%v\") to be %v", v.a, v.b, res))
  28. // If a == b then a != b
  29. expect(t, (v.a != v.b) == !res, fmt.tprintf("Expected cstring(\"%v\") != cstring(\"%v\") to be %v", v.a, v.b, !res))
  30. expect(t, (s_a != s_b) == !res, fmt.tprintf("Expected string(\"%v\") != string(\"%v\") to be %v", v.a, v.b, !res))
  31. case .Lt:
  32. expect(t, (v.a < v.b) == res, fmt.tprintf("Expected cstring(\"%v\") < cstring(\"%v\") to be %v", v.a, v.b, res))
  33. expect(t, (s_a < s_b) == res, fmt.tprintf("Expected string(\"%v\") < string(\"%v\") to be %v", v.a, v.b, res))
  34. // .Lt | .Eq == .LtEq
  35. lteq := v.res[.Eq] | res
  36. expect(t, (v.a <= v.b) == lteq, fmt.tprintf("Expected cstring(\"%v\") <= cstring(\"%v\") to be %v", v.a, v.b, lteq))
  37. expect(t, (s_a <= s_b) == lteq, fmt.tprintf("Expected string(\"%v\") <= string(\"%v\") to be %v", v.a, v.b, lteq))
  38. case .Gt:
  39. expect(t, (v.a > v.b) == res, fmt.tprintf("Expected cstring(\"%v\") > cstring(\"%v\") to be %v", v.a, v.b, res))
  40. expect(t, (s_a > s_b) == res, fmt.tprintf("Expected string(\"%v\") > string(\"%v\") to be %v", v.a, v.b, res))
  41. // .Gt | .Eq == .GtEq
  42. gteq := v.res[.Eq] | res
  43. expect(t, (v.a >= v.b) == gteq, fmt.tprintf("Expected cstring(\"%v\") >= cstring(\"%v\") to be %v", v.a, v.b, gteq))
  44. expect(t, (s_a >= s_b) == gteq, fmt.tprintf("Expected string(\"%v\") >= string(\"%v\") to be %v", v.a, v.b, gteq))
  45. }
  46. }
  47. }
  48. }
  49. // -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
  50. main :: proc() {
  51. t := testing.T{}
  52. string_compare(&t)
  53. fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
  54. if TEST_fail > 0 {
  55. os.exit(1)
  56. }
  57. }
  58. TEST_count := 0
  59. TEST_fail := 0
  60. when ODIN_TEST {
  61. expect :: testing.expect
  62. log :: testing.log
  63. } else {
  64. expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
  65. TEST_count += 1
  66. if !condition {
  67. TEST_fail += 1
  68. fmt.printf("[%v] %v\n", loc, message)
  69. return
  70. }
  71. }
  72. log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
  73. fmt.printf("[%v] ", loc)
  74. fmt.printf("log: %v\n", v)
  75. }
  76. }