test_mat.jl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # This file is adapted from test/abstractarray.jl from Julia.
  2. # This file is a part of Julia. License is MIT: https://julialang.org/license
  3. A_abs = rand(5,4,3)
  4. A = OpenCV.Mat(A_abs)
  5. @testset "Bounds checking" begin
  6. @test checkbounds(Bool, A, 1, 1, 1) == true
  7. @test checkbounds(Bool, A, 5, 4, 3) == true
  8. @test checkbounds(Bool, A, 0, 1, 1) == false
  9. @test checkbounds(Bool, A, 1, 0, 1) == false
  10. @test checkbounds(Bool, A, 1, 1, 0) == false
  11. @test checkbounds(Bool, A, 6, 4, 3) == false
  12. @test checkbounds(Bool, A, 5, 5, 3) == false
  13. @test checkbounds(Bool, A, 5, 4, 4) == false
  14. @test checkbounds(Bool, A, 1) == true # linear indexing
  15. @test checkbounds(Bool, A, 60) == true
  16. @test checkbounds(Bool, A, 61) == false
  17. @test checkbounds(Bool, A, 2, 2, 2, 1) == true # extra indices
  18. @test checkbounds(Bool, A, 2, 2, 2, 2) == false
  19. @test checkbounds(Bool, A, 1, 1) == false
  20. @test checkbounds(Bool, A, 1, 12) == false
  21. @test checkbounds(Bool, A, 5, 12) == false
  22. @test checkbounds(Bool, A, 1, 13) == false
  23. @test checkbounds(Bool, A, 6, 12) == false
  24. end
  25. @testset "single CartesianIndex" begin
  26. @test checkbounds(Bool, A, CartesianIndex((1, 1, 1))) == true
  27. @test checkbounds(Bool, A, CartesianIndex((5, 4, 3))) == true
  28. @test checkbounds(Bool, A, CartesianIndex((0, 1, 1))) == false
  29. @test checkbounds(Bool, A, CartesianIndex((1, 0, 1))) == false
  30. @test checkbounds(Bool, A, CartesianIndex((1, 1, 0))) == false
  31. @test checkbounds(Bool, A, CartesianIndex((6, 4, 3))) == false
  32. @test checkbounds(Bool, A, CartesianIndex((5, 5, 3))) == false
  33. @test checkbounds(Bool, A, CartesianIndex((5, 4, 4))) == false
  34. @test checkbounds(Bool, A, CartesianIndex((1,))) == false
  35. @test checkbounds(Bool, A, CartesianIndex((60,))) == false
  36. @test checkbounds(Bool, A, CartesianIndex((61,))) == false
  37. @test checkbounds(Bool, A, CartesianIndex((2, 2, 2, 1,))) == true
  38. @test checkbounds(Bool, A, CartesianIndex((2, 2, 2, 2,))) == false
  39. @test checkbounds(Bool, A, CartesianIndex((1, 1,))) == false
  40. @test checkbounds(Bool, A, CartesianIndex((1, 12,))) == false
  41. @test checkbounds(Bool, A, CartesianIndex((5, 12,))) == false
  42. @test checkbounds(Bool, A, CartesianIndex((1, 13,))) == false
  43. @test checkbounds(Bool, A, CartesianIndex((6, 12,))) == false
  44. end
  45. @testset "mix of CartesianIndex and Int" begin
  46. @test checkbounds(Bool, A, CartesianIndex((1,)), 1, CartesianIndex((1,))) == true
  47. @test checkbounds(Bool, A, CartesianIndex((5, 4)), 3) == true
  48. @test checkbounds(Bool, A, CartesianIndex((0, 1)), 1) == false
  49. @test checkbounds(Bool, A, 1, CartesianIndex((0, 1))) == false
  50. @test checkbounds(Bool, A, 1, 1, CartesianIndex((0,))) == false
  51. @test checkbounds(Bool, A, 6, CartesianIndex((4, 3))) == false
  52. @test checkbounds(Bool, A, 5, CartesianIndex((5,)), 3) == false
  53. @test checkbounds(Bool, A, CartesianIndex((5,)), CartesianIndex((4,)), CartesianIndex((4,))) == false
  54. end
  55. @testset "vector indices" begin
  56. @test checkbounds(Bool, A, 1:5, 1:4, 1:3) == true
  57. @test checkbounds(Bool, A, 0:5, 1:4, 1:3) == false
  58. @test checkbounds(Bool, A, 1:5, 0:4, 1:3) == false
  59. @test checkbounds(Bool, A, 1:5, 1:4, 0:3) == false
  60. @test checkbounds(Bool, A, 1:6, 1:4, 1:3) == false
  61. @test checkbounds(Bool, A, 1:5, 1:5, 1:3) == false
  62. @test checkbounds(Bool, A, 1:5, 1:4, 1:4) == false
  63. @test checkbounds(Bool, A, 1:60) == true
  64. @test checkbounds(Bool, A, 1:61) == false
  65. @test checkbounds(Bool, A, 2, 2, 2, 1:1) == true # extra indices
  66. @test checkbounds(Bool, A, 2, 2, 2, 1:2) == false
  67. @test checkbounds(Bool, A, 1:5, 1:4) == false
  68. @test checkbounds(Bool, A, 1:5, 1:12) == false
  69. @test checkbounds(Bool, A, 1:5, 1:13) == false
  70. @test checkbounds(Bool, A, 1:6, 1:12) == false
  71. end
  72. @testset "logical" begin
  73. @test checkbounds(Bool, A, trues(5), trues(4), trues(3)) == true
  74. @test checkbounds(Bool, A, trues(6), trues(4), trues(3)) == false
  75. @test checkbounds(Bool, A, trues(5), trues(5), trues(3)) == false
  76. @test checkbounds(Bool, A, trues(5), trues(4), trues(4)) == false
  77. @test checkbounds(Bool, A, trues(60)) == true
  78. @test checkbounds(Bool, A, trues(61)) == false
  79. @test checkbounds(Bool, A, 2, 2, 2, trues(1)) == true # extra indices
  80. @test checkbounds(Bool, A, 2, 2, 2, trues(2)) == false
  81. @test checkbounds(Bool, A, trues(5), trues(12)) == false
  82. @test checkbounds(Bool, A, trues(5), trues(13)) == false
  83. @test checkbounds(Bool, A, trues(6), trues(12)) == false
  84. @test checkbounds(Bool, A, trues(5, 4, 3)) == true
  85. @test checkbounds(Bool, A, trues(5, 4, 2)) == false
  86. @test checkbounds(Bool, A, trues(5, 12)) == false
  87. @test checkbounds(Bool, A, trues(1, 5), trues(1, 4, 1), trues(1, 1, 3)) == false
  88. @test checkbounds(Bool, A, trues(1, 5), trues(1, 4, 1), trues(1, 1, 2)) == false
  89. @test checkbounds(Bool, A, trues(1, 5), trues(1, 5, 1), trues(1, 1, 3)) == false
  90. @test checkbounds(Bool, A, trues(1, 5), :, 2) == false
  91. end
  92. @testset "array of CartesianIndex" begin
  93. @test checkbounds(Bool, A, [CartesianIndex((1, 1, 1))]) == true
  94. @test checkbounds(Bool, A, [CartesianIndex((5, 4, 3))]) == true
  95. @test checkbounds(Bool, A, [CartesianIndex((0, 1, 1))]) == false
  96. @test checkbounds(Bool, A, [CartesianIndex((1, 0, 1))]) == false
  97. @test checkbounds(Bool, A, [CartesianIndex((1, 1, 0))]) == false
  98. @test checkbounds(Bool, A, [CartesianIndex((6, 4, 3))]) == false
  99. @test checkbounds(Bool, A, [CartesianIndex((5, 5, 3))]) == false
  100. @test checkbounds(Bool, A, [CartesianIndex((5, 4, 4))]) == false
  101. @test checkbounds(Bool, A, [CartesianIndex((1, 1))], 1) == true
  102. @test checkbounds(Bool, A, [CartesianIndex((5, 4))], 3) == true
  103. @test checkbounds(Bool, A, [CartesianIndex((0, 1))], 1) == false
  104. @test checkbounds(Bool, A, [CartesianIndex((1, 0))], 1) == false
  105. @test checkbounds(Bool, A, [CartesianIndex((1, 1))], 0) == false
  106. @test checkbounds(Bool, A, [CartesianIndex((6, 4))], 3) == false
  107. @test checkbounds(Bool, A, [CartesianIndex((5, 5))], 3) == false
  108. @test checkbounds(Bool, A, [CartesianIndex((5, 4))], 4) == false
  109. end
  110. println("OpenCV.Mat tests passed")