test_core_strings.odin 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package test_core_strings
  2. import "core:strings"
  3. import "core:testing"
  4. import "core:fmt"
  5. import "core:os"
  6. import "core:runtime"
  7. TEST_count := 0
  8. TEST_fail := 0
  9. when ODIN_TEST {
  10. expect :: testing.expect
  11. log :: testing.log
  12. } else {
  13. expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
  14. TEST_count += 1
  15. if !condition {
  16. TEST_fail += 1
  17. fmt.printf("[%v] %v\n", loc, message)
  18. return
  19. }
  20. }
  21. log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
  22. fmt.printf("[%v] ", loc)
  23. fmt.printf("log: %v\n", v)
  24. }
  25. }
  26. main :: proc() {
  27. t := testing.T{}
  28. test_index_any_small_string_not_found(&t)
  29. test_index_any_larger_string_not_found(&t)
  30. test_index_any_small_string_found(&t)
  31. test_index_any_larger_string_found(&t)
  32. test_cut(&t)
  33. test_case_conversion(&t)
  34. fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
  35. if TEST_fail > 0 {
  36. os.exit(1)
  37. }
  38. }
  39. @test
  40. test_index_any_small_string_not_found :: proc(t: ^testing.T) {
  41. index := strings.index_any(".", "/:\"")
  42. expect(t, index == -1, "index_any should be negative")
  43. }
  44. @test
  45. test_index_any_larger_string_not_found :: proc(t: ^testing.T) {
  46. index := strings.index_any("aaaaaaaa.aaaaaaaa", "/:\"")
  47. expect(t, index == -1, "index_any should be negative")
  48. }
  49. @test
  50. test_index_any_small_string_found :: proc(t: ^testing.T) {
  51. index := strings.index_any(".", "/:.\"")
  52. expect(t, index == 0, "index_any should be 0")
  53. }
  54. @test
  55. test_index_any_larger_string_found :: proc(t: ^testing.T) {
  56. index := strings.index_any("aaaaaaaa:aaaaaaaa", "/:\"")
  57. expect(t, index == 8, "index_any should be 8")
  58. }
  59. Cut_Test :: struct {
  60. input: string,
  61. offset: int,
  62. length: int,
  63. output: string,
  64. }
  65. cut_tests :: []Cut_Test{
  66. {"some example text", 0, 4, "some" },
  67. {"some example text", 2, 2, "me" },
  68. {"some example text", 5, 7, "example" },
  69. {"some example text", 5, 0, "example text"},
  70. {"恥ずべきフクロウ", 4, 0, "フクロウ" },
  71. }
  72. @test
  73. test_cut :: proc(t: ^testing.T) {
  74. for test in cut_tests {
  75. res := strings.cut(test.input, test.offset, test.length)
  76. defer delete(res)
  77. msg := fmt.tprintf("cut(\"%v\", %v, %v) expected to return \"%v\", got \"%v\"",
  78. test.input, test.offset, test.length, test.output, res)
  79. expect(t, res == test.output, msg)
  80. }
  81. }
  82. Case_Kind :: enum {
  83. Lower_Space_Case,
  84. Upper_Space_Case,
  85. Lower_Snake_Case,
  86. Upper_Snake_Case,
  87. Lower_Kebab_Case,
  88. Upper_Kebab_Case,
  89. Camel_Case,
  90. Pascal_Case,
  91. Ada_Case,
  92. }
  93. test_cases := [Case_Kind]struct{s: string, p: proc(r: string, allocator: runtime.Allocator) -> string}{
  94. .Lower_Space_Case = {"hellope world", to_lower_space_case},
  95. .Upper_Space_Case = {"HELLOPE WORLD", to_upper_space_case},
  96. .Lower_Snake_Case = {"hellope_world", strings.to_snake_case},
  97. .Upper_Snake_Case = {"HELLOPE_WORLD", strings.to_upper_snake_case},
  98. .Lower_Kebab_Case = {"hellope-world", strings.to_kebab_case},
  99. .Upper_Kebab_Case = {"HELLOPE-WORLD", strings.to_upper_kebab_case},
  100. .Camel_Case = {"hellopeWorld", strings.to_camel_case},
  101. .Pascal_Case = {"HellopeWorld", strings.to_pascal_case},
  102. .Ada_Case = {"Hellope_World", strings.to_ada_case},
  103. }
  104. to_lower_space_case :: proc(r: string, allocator: runtime.Allocator) -> string {
  105. return strings.to_delimiter_case(r, ' ', false, allocator)
  106. }
  107. to_upper_space_case :: proc(r: string, allocator: runtime.Allocator) -> string {
  108. return strings.to_delimiter_case(r, ' ', true, allocator)
  109. }
  110. @test
  111. test_case_conversion :: proc(t: ^testing.T) {
  112. for entry in test_cases {
  113. for test_case, case_kind in test_cases {
  114. result := entry.p(test_case.s, context.allocator)
  115. defer delete(result)
  116. msg := fmt.tprintf("ERROR: Input `{}` to converter {} does not match `{}`, got `{}`.\n", test_case.s, case_kind, entry.s, result)
  117. expect(t, result == entry.s, msg)
  118. }
  119. }
  120. }