test_string_compare.odin 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package test_internal
  2. import "core:testing"
  3. Op :: enum { Eq, Lt, Gt }
  4. Test :: struct {
  5. a: cstring,
  6. b: cstring,
  7. res: [Op]bool,
  8. }
  9. CASES := []Test{
  10. {"hellope", "hellope", {.Eq=true, .Lt=false, .Gt=false}},
  11. {"Hellope", "hellope", {.Eq=false, .Lt=true, .Gt=false}}, // H < h
  12. {"Hell", "Hellope", {.Eq=false, .Lt=true, .Gt=false}},
  13. {"Hellope!", "Hellope", {.Eq=false, .Lt=false, .Gt=true }},
  14. {"Hellopf", "Hellope", {.Eq=false, .Lt=false, .Gt=true }},
  15. }
  16. @test
  17. string_compare :: proc(t: ^testing.T) {
  18. for v in CASES {
  19. s_a := string(v.a)
  20. s_b := string(v.b)
  21. for res, op in v.res {
  22. switch op {
  23. case .Eq:
  24. testing.expectf(t, (v.a == v.b) == res, "Expected cstring(\"%v\") == cstring(\"%v\") to be %v", v.a, v.b, res)
  25. testing.expectf(t, (s_a == s_b) == res, "Expected string(\"%v\") == string(\"%v\") to be %v", v.a, v.b, res)
  26. // If a == b then a != b
  27. testing.expectf(t, (v.a != v.b) == !res, "Expected cstring(\"%v\") != cstring(\"%v\") to be %v", v.a, v.b, !res)
  28. testing.expectf(t, (s_a != s_b) == !res, "Expected string(\"%v\") != string(\"%v\") to be %v", v.a, v.b, !res)
  29. case .Lt:
  30. testing.expectf(t, (v.a < v.b) == res, "Expected cstring(\"%v\") < cstring(\"%v\") to be %v", v.a, v.b, res)
  31. testing.expectf(t, (s_a < s_b) == res, "Expected string(\"%v\") < string(\"%v\") to be %v", v.a, v.b, res)
  32. // .Lt | .Eq == .LtEq
  33. lteq := v.res[.Eq] | res
  34. testing.expectf(t, (v.a <= v.b) == lteq, "Expected cstring(\"%v\") <= cstring(\"%v\") to be %v", v.a, v.b, lteq)
  35. testing.expectf(t, (s_a <= s_b) == lteq, "Expected string(\"%v\") <= string(\"%v\") to be %v", v.a, v.b, lteq)
  36. case .Gt:
  37. testing.expectf(t, (v.a > v.b) == res, "Expected cstring(\"%v\") > cstring(\"%v\") to be %v", v.a, v.b, res)
  38. testing.expectf(t, (s_a > s_b) == res, "Expected string(\"%v\") > string(\"%v\") to be %v", v.a, v.b, res)
  39. // .Gt | .Eq == .GtEq
  40. gteq := v.res[.Eq] | res
  41. testing.expectf(t, (v.a >= v.b) == gteq, "Expected cstring(\"%v\") >= cstring(\"%v\") to be %v", v.a, v.b, gteq)
  42. testing.expectf(t, (s_a >= s_b) == gteq, "Expected string(\"%v\") >= string(\"%v\") to be %v", v.a, v.b, gteq)
  43. }
  44. }
  45. }
  46. }