test_core_bytes.odin 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package test_core_bytes
  2. import "core:bytes"
  3. import "core:slice"
  4. import "core:testing"
  5. @private SIMD_SCAN_WIDTH :: 8 * size_of(uintptr)
  6. @test
  7. test_index_byte_sanity :: proc(t: ^testing.T) {
  8. // We must be able to find the byte at the correct index.
  9. data := make([]u8, 2 * SIMD_SCAN_WIDTH)
  10. defer delete(data)
  11. slice.fill(data, '-')
  12. INDEX_MAX :: SIMD_SCAN_WIDTH - 1
  13. for offset in 0..<INDEX_MAX {
  14. for idx in 0..<INDEX_MAX {
  15. sub := data[offset:]
  16. sub[idx] = 'o'
  17. if !testing.expect_value(t, bytes.index_byte(sub, 'o'), idx) {
  18. return
  19. }
  20. if !testing.expect_value(t, bytes.last_index_byte(sub, 'o'), idx) {
  21. return
  22. }
  23. sub[idx] = '-'
  24. }
  25. }
  26. }
  27. @test
  28. test_index_byte_empty :: proc(t: ^testing.T) {
  29. a: [1]u8
  30. testing.expect_value(t, bytes.index_byte(a[0:0], 'o'), -1)
  31. testing.expect_value(t, bytes.last_index_byte(a[0:0], 'o'), -1)
  32. }
  33. @test
  34. test_index_byte_multiple_hits :: proc(t: ^testing.T) {
  35. for n in 5..<256 {
  36. data := make([]u8, n)
  37. defer delete(data)
  38. slice.fill(data, '-')
  39. data[n-1] = 'o'
  40. data[n-3] = 'o'
  41. data[n-5] = 'o'
  42. // Find the first one.
  43. if !testing.expect_value(t, bytes.index_byte(data, 'o'), n-5) {
  44. return
  45. }
  46. // Find the last one.
  47. if !testing.expect_value(t, bytes.last_index_byte(data, 'o'), n-1) {
  48. return
  49. }
  50. }
  51. }
  52. @test
  53. test_index_byte_zero :: proc(t: ^testing.T) {
  54. // This test protects against false positives in uninitialized memory.
  55. for n in 1..<256 {
  56. data := make([]u8, n + 64)
  57. defer delete(data)
  58. slice.fill(data, '-')
  59. // Positive hit.
  60. data[n-1] = 0
  61. if !testing.expect_value(t, bytes.index_byte(data[:n], 0), n-1) {
  62. return
  63. }
  64. if !testing.expect_value(t, bytes.last_index_byte(data[:n], 0), n-1) {
  65. return
  66. }
  67. // Test for false positives.
  68. data[n-1] = '-'
  69. if !testing.expect_value(t, bytes.index_byte(data[:n], 0), -1) {
  70. return
  71. }
  72. if !testing.expect_value(t, bytes.last_index_byte(data[:n], 0), -1) {
  73. return
  74. }
  75. }
  76. }
  77. @test
  78. test_last_index_byte_bounds :: proc(t: ^testing.T) {
  79. input := "helloworld.odin."
  80. assert(len(input) == 16)
  81. idx := bytes.last_index_byte(transmute([]byte)(input[:len(input)-1]), '.')
  82. testing.expect_value(t, idx, 10)
  83. }