|
83 | 83 | @test_throws ArgumentError partialsortperm!([1,2], [2,3,1], 1:2)
|
84 | 84 | end
|
85 | 85 |
|
86 |
| -@testset "searchsorted" begin |
87 |
| - numTypes = [ Int8, Int16, Int32, Int64, Int128, |
88 |
| - UInt8, UInt16, UInt32, UInt64, UInt128, |
89 |
| - Float16, Float32, Float64, BigInt, BigFloat] |
90 |
| - |
91 |
| - @test searchsorted([1:10;], 1, by=(x -> x >= 5)) == 1:4 |
92 |
| - @test searchsorted([1:10;], 10, by=(x -> x >= 5)) == 5:10 |
93 |
| - @test searchsorted([1:5; 1:5; 1:5], 1, 6, 10, Forward) == 6:6 |
94 |
| - @test searchsorted(fill(1, 15), 1, 6, 10, Forward) == 6:10 |
95 |
| - |
96 |
| - for R in numTypes, T in numTypes |
97 |
| - @test searchsorted(R[1, 1, 2, 2, 3, 3], T(0)) === 1:0 |
98 |
| - @test searchsorted(R[1, 1, 2, 2, 3, 3], T(1)) == 1:2 |
99 |
| - @test searchsorted(R[1, 1, 2, 2, 3, 3], T(2)) == 3:4 |
100 |
| - @test searchsorted(R[1, 1, 2, 2, 3, 3], T(4)) === 7:6 |
101 |
| - @test searchsorted(R[1, 1, 2, 2, 3, 3], 2.5) === 5:4 |
102 |
| - |
103 |
| - @test searchsorted(1:3, T(0)) === 1:0 |
104 |
| - @test searchsorted(1:3, T(1)) == 1:1 |
105 |
| - @test searchsorted(1:3, T(2)) == 2:2 |
106 |
| - @test searchsorted(1:3, T(4)) === 4:3 |
107 |
| - |
108 |
| - @test searchsorted(R[1:10;], T(1), by=(x -> x >= 5)) == 1:4 |
109 |
| - @test searchsorted(R[1:10;], T(10), by=(x -> x >= 5)) == 5:10 |
110 |
| - @test searchsorted(R[1:5; 1:5; 1:5], T(1), 6, 10, Forward) == 6:6 |
111 |
| - @test searchsorted(fill(R(1), 15), T(1), 6, 10, Forward) == 6:10 |
112 |
| - end |
113 |
| - |
114 |
| - for (rg,I) in Any[(49:57,47:59), (1:2:17,-1:19), (-3:0.5:2,-5:.5:4)] |
115 |
| - rg_r = reverse(rg) |
116 |
| - rgv, rgv_r = [rg;], [rg_r;] |
117 |
| - for i = I |
118 |
| - @test searchsorted(rg,i) === searchsorted(rgv,i) |
119 |
| - @test searchsorted(rg_r,i,rev=true) === searchsorted(rgv_r,i,rev=true) |
120 |
| - end |
121 |
| - end |
122 |
| - |
123 |
| - rg = 0.0:0.01:1.0 |
124 |
| - for i = 2:101 |
125 |
| - @test searchsorted(rg, rg[i]) == i:i |
126 |
| - @test searchsorted(rg, prevfloat(rg[i])) === i:i-1 |
127 |
| - @test searchsorted(rg, nextfloat(rg[i])) === i+1:i |
128 |
| - end |
129 |
| - |
130 |
| - rg_r = reverse(rg) |
131 |
| - for i = 1:100 |
132 |
| - @test searchsorted(rg_r, rg_r[i], rev=true) == i:i |
133 |
| - @test searchsorted(rg_r, prevfloat(rg_r[i]), rev=true) === i+1:i |
134 |
| - @test searchsorted(rg_r, nextfloat(rg_r[i]), rev=true) === i:i-1 |
135 |
| - end |
136 |
| - |
137 |
| - @test searchsorted(1:10, 1, by=(x -> x >= 5)) == searchsorted([1:10;], 1, by=(x -> x >= 5)) |
138 |
| - @test searchsorted(1:10, 10, by=(x -> x >= 5)) == searchsorted([1:10;], 10, by=(x -> x >= 5)) |
139 |
| - |
140 |
| - @test searchsorted([], 0) === 1:0 |
141 |
| - @test searchsorted([1,2,3], 0) === 1:0 |
142 |
| - @test searchsorted([1,2,3], 4) === 4:3 |
143 |
| - |
144 |
| - @testset "issue 8866" begin |
145 |
| - @test searchsortedfirst(500:1.0:600, -1.0e20) == 1 |
146 |
| - @test searchsortedfirst(500:1.0:600, 1.0e20) == 102 |
147 |
| - @test searchsortedlast(500:1.0:600, -1.0e20) == 0 |
148 |
| - @test searchsortedlast(500:1.0:600, 1.0e20) == 101 |
149 |
| - end |
150 |
| - |
151 |
| - @testset "issue 10966" begin |
152 |
| - for R in numTypes, T in numTypes |
153 |
| - @test searchsortedfirst(R(2):R(2), T(0)) == 1 |
154 |
| - @test searchsortedfirst(R(2):R(2), T(2)) == 1 |
155 |
| - @test searchsortedfirst(R(2):R(2), T(3)) == 2 |
156 |
| - @test searchsortedfirst(R(1):1//2:R(5), T(0)) == 1 |
157 |
| - @test searchsortedfirst(R(1):1//2:R(5), T(2)) == 3 |
158 |
| - @test searchsortedfirst(R(1):1//2:R(5), T(6)) == 10 |
159 |
| - @test searchsortedlast(R(2):R(2), T(0)) == 0 |
160 |
| - @test searchsortedlast(R(2):R(2), T(2)) == 1 |
161 |
| - @test searchsortedlast(R(2):R(2), T(3)) == 1 |
162 |
| - @test searchsortedlast(R(1):1//2:R(5), T(0)) == 0 |
163 |
| - @test searchsortedlast(R(1):1//2:R(5), T(2)) == 3 |
164 |
| - @test searchsortedlast(R(1):1//2:R(5), T(6)) == 9 |
165 |
| - @test searchsorted(R(2):R(2), T(0)) === 1:0 |
166 |
| - @test searchsorted(R(2):R(2), T(2)) == 1:1 |
167 |
| - @test searchsorted(R(2):R(2), T(3)) === 2:1 |
168 |
| - end |
169 |
| - end |
170 |
| - |
171 |
| - @testset "issue 32568" begin |
172 |
| - for R in numTypes, T in numTypes |
173 |
| - for arr in Any[R[1:5;], R(1):R(5), R(1):2:R(5)] |
174 |
| - @test eltype(searchsorted(arr, T(2))) == keytype(arr) |
175 |
| - @test eltype(searchsorted(arr, T(2), big(1), big(4), Forward)) == keytype(arr) |
176 |
| - @test searchsortedfirst(arr, T(2)) isa keytype(arr) |
177 |
| - @test searchsortedfirst(arr, T(2), big(1), big(4), Forward) isa keytype(arr) |
178 |
| - @test searchsortedlast(arr, T(2)) isa keytype(arr) |
179 |
| - @test searchsortedlast(arr, T(2), big(1), big(4), Forward) isa keytype(arr) |
180 |
| - end |
181 |
| - end |
182 |
| - end |
183 |
| - |
184 |
| - @testset "issue #34157" begin |
185 |
| - @test searchsorted(1:2.0, -Inf) === 1:0 |
186 |
| - @test searchsorted([1,2], -Inf) === 1:0 |
187 |
| - @test searchsorted(1:2, -Inf) === 1:0 |
188 |
| - |
189 |
| - @test searchsorted(1:2.0, Inf) === 3:2 |
190 |
| - @test searchsorted([1,2], Inf) === 3:2 |
191 |
| - @test searchsorted(1:2, Inf) === 3:2 |
192 |
| - |
193 |
| - for coll in Any[ |
194 |
| - Base.OneTo(10), |
195 |
| - 1:2, |
196 |
| - 0x01:0x02, |
197 |
| - -4:6, |
198 |
| - 5:2:10, |
199 |
| - [1,2], |
200 |
| - 1.0:4, |
201 |
| - [10.0,20.0], |
202 |
| - ] |
203 |
| - for huge in Any[Inf, 1e300, typemax(Int64), typemax(UInt64)] |
204 |
| - @test searchsortedfirst(coll, huge) === lastindex(coll) + 1 |
205 |
| - @test searchsortedlast(coll, huge) === lastindex(coll) |
206 |
| - @test searchsorted(coll, huge) === lastindex(coll)+1 : lastindex(coll) |
207 |
| - if !(eltype(coll) <: Unsigned) |
208 |
| - @test searchsortedfirst(reverse(coll), huge, rev=true) === firstindex(coll) |
209 |
| - @test searchsortedlast(reverse(coll), huge, rev=true) === firstindex(coll) - 1 |
210 |
| - @test searchsorted(reverse(coll), huge, rev=true) === firstindex(coll):firstindex(coll) - 1 |
211 |
| - end |
212 |
| - |
213 |
| - if !(huge isa Unsigned) |
214 |
| - @test searchsortedfirst(coll, -huge)=== firstindex(coll) |
215 |
| - @test searchsortedlast(coll, -huge) === firstindex(coll) - 1 |
216 |
| - @test searchsorted(coll, -huge) === firstindex(coll) : firstindex(coll) - 1 |
217 |
| - if !(eltype(coll) <: Unsigned) |
218 |
| - @test searchsortedfirst(reverse(coll), -huge, rev=true) === lastindex(coll) + 1 |
219 |
| - @test searchsortedlast(reverse(coll), -huge, rev=true) === lastindex(coll) |
220 |
| - @test searchsorted(reverse(coll), -huge, rev=true) === lastindex(coll)+1:lastindex(coll) |
221 |
| - end |
222 |
| - end |
223 |
| - end |
224 |
| - end |
225 |
| - |
226 |
| - @testset "issue #34408" begin |
227 |
| - r = 1f8-10:1f8 |
228 |
| - # collect(r) = Float32[9.999999e7, 9.999999e7, 9.999999e7, 9.999999e7, 1.0e8, 1.0e8, 1.0e8, 1.0e8, 1.0e8] |
229 |
| - for i in r |
230 |
| - @test_broken searchsorted(collect(r), i) == searchsorted(r, i) |
231 |
| - end |
232 |
| - end |
233 |
| - end |
234 |
| - @testset "issue #35272" begin |
235 |
| - for v0 = (3:-1:1, 3.0:-1.0:1.0), v = (v0, collect(v0)) |
236 |
| - @test searchsorted(v, 3, rev=true) == 1:1 |
237 |
| - @test searchsorted(v, 3.0, rev=true) == 1:1 |
238 |
| - @test searchsorted(v, 2.5, rev=true) === 2:1 |
239 |
| - @test searchsorted(v, 2, rev=true) == 2:2 |
240 |
| - @test searchsorted(v, 1.2, rev=true) === 3:2 |
241 |
| - @test searchsorted(v, 1, rev=true) == 3:3 |
242 |
| - @test searchsorted(v, 0.1, rev=true) === 4:3 |
243 |
| - end |
244 |
| - end |
245 |
| -end |
246 | 86 | # exercise the codepath in searchsorted* methods for ranges that check for zero step range
|
247 | 87 | struct ConstantRange{T} <: AbstractRange{T}
|
248 | 88 | val::T
|
@@ -814,4 +654,165 @@ end
|
814 | 654 | end
|
815 | 655 | end
|
816 | 656 |
|
| 657 | +@testset "searchsorted" begin |
| 658 | + numTypes = [ Int8, Int16, Int32, Int64, Int128, |
| 659 | + UInt8, UInt16, UInt32, UInt64, UInt128, |
| 660 | + Float16, Float32, Float64, BigInt, BigFloat] |
| 661 | + |
| 662 | + @test searchsorted([1:10;], 1, by=(x -> x >= 5)) == 1:4 |
| 663 | + @test searchsorted([1:10;], 10, by=(x -> x >= 5)) == 5:10 |
| 664 | + @test searchsorted([1:5; 1:5; 1:5], 1, 6, 10, Forward) == 6:6 |
| 665 | + @test searchsorted(fill(1, 15), 1, 6, 10, Forward) == 6:10 |
| 666 | + |
| 667 | + for R in numTypes, T in numTypes |
| 668 | + @test searchsorted(R[1, 1, 2, 2, 3, 3], T(0)) === 1:0 |
| 669 | + @test searchsorted(R[1, 1, 2, 2, 3, 3], T(1)) == 1:2 |
| 670 | + @test searchsorted(R[1, 1, 2, 2, 3, 3], T(2)) == 3:4 |
| 671 | + @test searchsorted(R[1, 1, 2, 2, 3, 3], T(4)) === 7:6 |
| 672 | + @test searchsorted(R[1, 1, 2, 2, 3, 3], 2.5) === 5:4 |
| 673 | + |
| 674 | + @test searchsorted(1:3, T(0)) === 1:0 |
| 675 | + @test searchsorted(1:3, T(1)) == 1:1 |
| 676 | + @test searchsorted(1:3, T(2)) == 2:2 |
| 677 | + @test searchsorted(1:3, T(4)) === 4:3 |
| 678 | + |
| 679 | + @test searchsorted(R[1:10;], T(1), by=(x -> x >= 5)) == 1:4 |
| 680 | + @test searchsorted(R[1:10;], T(10), by=(x -> x >= 5)) == 5:10 |
| 681 | + @test searchsorted(R[1:5; 1:5; 1:5], T(1), 6, 10, Forward) == 6:6 |
| 682 | + @test searchsorted(fill(R(1), 15), T(1), 6, 10, Forward) == 6:10 |
| 683 | + end |
| 684 | + |
| 685 | + for (rg,I) in Any[(49:57,47:59), (1:2:17,-1:19), (-3:0.5:2,-5:.5:4)] |
| 686 | + rg_r = reverse(rg) |
| 687 | + rgv, rgv_r = [rg;], [rg_r;] |
| 688 | + for i = I |
| 689 | + @test searchsorted(rg,i) === searchsorted(rgv,i) |
| 690 | + @test searchsorted(rg_r,i,rev=true) === searchsorted(rgv_r,i,rev=true) |
| 691 | + end |
| 692 | + end |
| 693 | + |
| 694 | + rg = 0.0:0.01:1.0 |
| 695 | + for i = 2:101 |
| 696 | + @test searchsorted(rg, rg[i]) == i:i |
| 697 | + @test searchsorted(rg, prevfloat(rg[i])) === i:i-1 |
| 698 | + @test searchsorted(rg, nextfloat(rg[i])) === i+1:i |
| 699 | + end |
| 700 | + |
| 701 | + rg_r = reverse(rg) |
| 702 | + for i = 1:100 |
| 703 | + @test searchsorted(rg_r, rg_r[i], rev=true) == i:i |
| 704 | + @test searchsorted(rg_r, prevfloat(rg_r[i]), rev=true) === i+1:i |
| 705 | + @test searchsorted(rg_r, nextfloat(rg_r[i]), rev=true) === i:i-1 |
| 706 | + end |
| 707 | + |
| 708 | + @test searchsorted(1:10, 1, by=(x -> x >= 5)) == searchsorted([1:10;], 1, by=(x -> x >= 5)) |
| 709 | + @test searchsorted(1:10, 10, by=(x -> x >= 5)) == searchsorted([1:10;], 10, by=(x -> x >= 5)) |
| 710 | + |
| 711 | + @test searchsorted([], 0) === 1:0 |
| 712 | + @test searchsorted([1,2,3], 0) === 1:0 |
| 713 | + @test searchsorted([1,2,3], 4) === 4:3 |
| 714 | + |
| 715 | + @testset "issue 8866" begin |
| 716 | + @test searchsortedfirst(500:1.0:600, -1.0e20) == 1 |
| 717 | + @test searchsortedfirst(500:1.0:600, 1.0e20) == 102 |
| 718 | + @test searchsortedlast(500:1.0:600, -1.0e20) == 0 |
| 719 | + @test searchsortedlast(500:1.0:600, 1.0e20) == 101 |
| 720 | + end |
| 721 | + |
| 722 | + @testset "issue 10966" begin |
| 723 | + for R in numTypes, T in numTypes |
| 724 | + @test searchsortedfirst(R(2):R(2), T(0)) == 1 |
| 725 | + @test searchsortedfirst(R(2):R(2), T(2)) == 1 |
| 726 | + @test searchsortedfirst(R(2):R(2), T(3)) == 2 |
| 727 | + @test searchsortedfirst(R(1):1//2:R(5), T(0)) == 1 |
| 728 | + @test searchsortedfirst(R(1):1//2:R(5), T(2)) == 3 |
| 729 | + @test searchsortedfirst(R(1):1//2:R(5), T(6)) == 10 |
| 730 | + @test searchsortedlast(R(2):R(2), T(0)) == 0 |
| 731 | + @test searchsortedlast(R(2):R(2), T(2)) == 1 |
| 732 | + @test searchsortedlast(R(2):R(2), T(3)) == 1 |
| 733 | + @test searchsortedlast(R(1):1//2:R(5), T(0)) == 0 |
| 734 | + @test searchsortedlast(R(1):1//2:R(5), T(2)) == 3 |
| 735 | + @test searchsortedlast(R(1):1//2:R(5), T(6)) == 9 |
| 736 | + @test searchsorted(R(2):R(2), T(0)) === 1:0 |
| 737 | + @test searchsorted(R(2):R(2), T(2)) == 1:1 |
| 738 | + @test searchsorted(R(2):R(2), T(3)) === 2:1 |
| 739 | + end |
| 740 | + end |
| 741 | + |
| 742 | + @testset "issue 32568" begin |
| 743 | + for R in numTypes, T in numTypes |
| 744 | + for arr in Any[R[1:5;], R(1):R(5), R(1):2:R(5)] |
| 745 | + @test eltype(searchsorted(arr, T(2))) == keytype(arr) |
| 746 | + @test eltype(searchsorted(arr, T(2), big(1), big(4), Forward)) == keytype(arr) |
| 747 | + @test searchsortedfirst(arr, T(2)) isa keytype(arr) |
| 748 | + @test searchsortedfirst(arr, T(2), big(1), big(4), Forward) isa keytype(arr) |
| 749 | + @test searchsortedlast(arr, T(2)) isa keytype(arr) |
| 750 | + @test searchsortedlast(arr, T(2), big(1), big(4), Forward) isa keytype(arr) |
| 751 | + end |
| 752 | + end |
| 753 | + end |
| 754 | + |
| 755 | + @testset "issue #34157" begin |
| 756 | + @test searchsorted(1:2.0, -Inf) === 1:0 |
| 757 | + @test searchsorted([1,2], -Inf) === 1:0 |
| 758 | + @test searchsorted(1:2, -Inf) === 1:0 |
| 759 | + |
| 760 | + @test searchsorted(1:2.0, Inf) === 3:2 |
| 761 | + @test searchsorted([1,2], Inf) === 3:2 |
| 762 | + @test searchsorted(1:2, Inf) === 3:2 |
| 763 | + |
| 764 | + for coll in Any[ |
| 765 | + Base.OneTo(10), |
| 766 | + 1:2, |
| 767 | + 0x01:0x02, |
| 768 | + -4:6, |
| 769 | + 5:2:10, |
| 770 | + [1,2], |
| 771 | + 1.0:4, |
| 772 | + [10.0,20.0], |
| 773 | + ] |
| 774 | + for huge in Any[Inf, 1e300, typemax(Int64), typemax(UInt64)] |
| 775 | + @test searchsortedfirst(coll, huge) === lastindex(coll) + 1 |
| 776 | + @test searchsortedlast(coll, huge) === lastindex(coll) |
| 777 | + @test searchsorted(coll, huge) === lastindex(coll)+1 : lastindex(coll) |
| 778 | + if !(eltype(coll) <: Unsigned) |
| 779 | + @test searchsortedfirst(reverse(coll), huge, rev=true) === firstindex(coll) |
| 780 | + @test searchsortedlast(reverse(coll), huge, rev=true) === firstindex(coll) - 1 |
| 781 | + @test searchsorted(reverse(coll), huge, rev=true) === firstindex(coll):firstindex(coll) - 1 |
| 782 | + end |
| 783 | + |
| 784 | + if !(huge isa Unsigned) |
| 785 | + @test searchsortedfirst(coll, -huge)=== firstindex(coll) |
| 786 | + @test searchsortedlast(coll, -huge) === firstindex(coll) - 1 |
| 787 | + @test searchsorted(coll, -huge) === firstindex(coll) : firstindex(coll) - 1 |
| 788 | + if !(eltype(coll) <: Unsigned) |
| 789 | + @test searchsortedfirst(reverse(coll), -huge, rev=true) === lastindex(coll) + 1 |
| 790 | + @test searchsortedlast(reverse(coll), -huge, rev=true) === lastindex(coll) |
| 791 | + @test searchsorted(reverse(coll), -huge, rev=true) === lastindex(coll)+1:lastindex(coll) |
| 792 | + end |
| 793 | + end |
| 794 | + end |
| 795 | + end |
| 796 | + |
| 797 | + @testset "issue #34408" begin |
| 798 | + r = 1f8-10:1f8 |
| 799 | + # collect(r) = Float32[9.999999e7, 9.999999e7, 9.999999e7, 9.999999e7, 1.0e8, 1.0e8, 1.0e8, 1.0e8, 1.0e8] |
| 800 | + for i in r |
| 801 | + @test_broken searchsorted(collect(r), i) == searchsorted(r, i) |
| 802 | + end |
| 803 | + end |
| 804 | + end |
| 805 | + @testset "issue #35272" begin |
| 806 | + for v0 = (3:-1:1, 3.0:-1.0:1.0), v = (v0, collect(v0)) |
| 807 | + @test searchsorted(v, 3, rev=true) == 1:1 |
| 808 | + @test searchsorted(v, 3.0, rev=true) == 1:1 |
| 809 | + @test searchsorted(v, 2.5, rev=true) === 2:1 |
| 810 | + @test searchsorted(v, 2, rev=true) == 2:2 |
| 811 | + @test searchsorted(v, 1.2, rev=true) === 3:2 |
| 812 | + @test searchsorted(v, 1, rev=true) == 3:3 |
| 813 | + @test searchsorted(v, 0.1, rev=true) === 4:3 |
| 814 | + end |
| 815 | + end |
| 816 | +end |
| 817 | + |
817 | 818 | end
|
0 commit comments