test_core_strings.odin 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package test_core_strings
  2. import "core:strings"
  3. import "core:testing"
  4. import "core:fmt"
  5. import "core:os"
  6. TEST_count := 0
  7. TEST_fail := 0
  8. when ODIN_TEST {
  9. expect :: testing.expect
  10. log :: testing.log
  11. } else {
  12. expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
  13. TEST_count += 1
  14. if !condition {
  15. TEST_fail += 1
  16. fmt.printf("[%v] %v\n", loc, message)
  17. return
  18. }
  19. }
  20. log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
  21. fmt.printf("[%v] ", loc)
  22. fmt.printf("log: %v\n", v)
  23. }
  24. }
  25. main :: proc() {
  26. t := testing.T{}
  27. test_index_any_small_string_not_found(&t)
  28. test_index_any_larger_string_not_found(&t)
  29. test_index_any_small_string_found(&t)
  30. test_index_any_larger_string_found(&t)
  31. test_cut(&t)
  32. fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
  33. if TEST_fail > 0 {
  34. os.exit(1)
  35. }
  36. }
  37. @test
  38. test_index_any_small_string_not_found :: proc(t: ^testing.T) {
  39. index := strings.index_any(".", "/:\"")
  40. expect(t, index == -1, "index_any should be negative")
  41. }
  42. @test
  43. test_index_any_larger_string_not_found :: proc(t: ^testing.T) {
  44. index := strings.index_any("aaaaaaaa.aaaaaaaa", "/:\"")
  45. expect(t, index == -1, "index_any should be negative")
  46. }
  47. @test
  48. test_index_any_small_string_found :: proc(t: ^testing.T) {
  49. index := strings.index_any(".", "/:.\"")
  50. expect(t, index == 0, "index_any should be 0")
  51. }
  52. @test
  53. test_index_any_larger_string_found :: proc(t: ^testing.T) {
  54. index := strings.index_any("aaaaaaaa:aaaaaaaa", "/:\"")
  55. expect(t, index == 8, "index_any should be 8")
  56. }
  57. Cut_Test :: struct {
  58. input: string,
  59. offset: int,
  60. length: int,
  61. output: string,
  62. }
  63. cut_tests :: []Cut_Test{
  64. {"some example text", 0, 4, "some" },
  65. {"some example text", 2, 2, "me" },
  66. {"some example text", 5, 7, "example" },
  67. {"some example text", 5, 0, "example text"},
  68. {"恥ずべきフクロウ", 4, 0, "フクロウ" },
  69. }
  70. @test
  71. test_cut :: proc(t: ^testing.T) {
  72. for test in cut_tests {
  73. res := strings.cut(test.input, test.offset, test.length)
  74. defer delete(res)
  75. msg := fmt.tprintf("cut(\"%v\", %v, %v) expected to return \"%v\", got \"%v\"",
  76. test.input, test.offset, test.length, test.output, res)
  77. expect(t, res == test.output, msg)
  78. }
  79. }