mirror of
https://github.com/revarbat/BOSL2.git
synced 2025-08-25 07:10:41 +02:00
Brought slice() in line with select() indexing, without wrapping. Replaced a lot of select() and slice() calls with last(), list_head(), and list_tail() calls.
This commit is contained in:
@@ -29,12 +29,14 @@ test_select();
|
||||
|
||||
|
||||
module test_slice() {
|
||||
assert(slice([3,4,5,6,7,8,9], 3, 5) == [6,7]);
|
||||
assert(slice([3,4,5,6,7,8,9], 2, -1) == [5,6,7,8,9]);
|
||||
assert(slice([3,4,5,6,7,8,9], 1, 1) == []);
|
||||
assert(slice([3,4,5,6,7,8,9], 6, -1) == [9]);
|
||||
assert(slice([3,4,5,6,7,8,9], 2, -2) == [5,6,7,8]);
|
||||
assert(slice([], 2, -2) == []);
|
||||
l = [3,4,5,6,7,8,9];
|
||||
assert(slice(l, 5, 6) == [8,9]);
|
||||
assert(slice(l, 5, 8) == [8,9]);
|
||||
assert(slice(l, 5, 2) == []);
|
||||
assert(slice(l, -3, -1) == [7,8,9]);
|
||||
assert(slice(l, 3, 3) == [6]);
|
||||
assert(slice(l, 4) == [7,8,9]);
|
||||
assert(slice(l, -2) == [8,9]);
|
||||
}
|
||||
test_slice();
|
||||
|
||||
@@ -131,19 +133,36 @@ module test_repeat() {
|
||||
test_repeat();
|
||||
|
||||
|
||||
module test_list_range() {
|
||||
assert(list_range(4) == [0,1,2,3]);
|
||||
assert(list_range(n=4, step=2) == [0,2,4,6]);
|
||||
assert(list_range(n=4, s=3, step=3) == [3,6,9,12]);
|
||||
assert(list_range(e=3) == [0,1,2,3]);
|
||||
assert(list_range(e=6, step=2) == [0,2,4,6]);
|
||||
assert(list_range(s=3, e=5) == [3,4,5]);
|
||||
assert(list_range(s=3, e=8, step=2) == [3,5,7]);
|
||||
assert(list_range(s=4, e=8, step=2) == [4,6,8]);
|
||||
assert(list_range(e=4, n=3) == [0,2,4]);
|
||||
assert(list_range(n=4, s=[3,4], step=[2,3]) == [[3,4], [5,7], [7,10], [9,13]]);
|
||||
module test_range() {
|
||||
assert(range(4) == [0,1,2,3]);
|
||||
assert(range(n=4, step=2) == [0,2,4,6]);
|
||||
assert(range(n=4, s=3, step=3) == [3,6,9,12]);
|
||||
assert(range(e=3) == [0,1,2,3]);
|
||||
assert(range(e=6, step=2) == [0,2,4,6]);
|
||||
assert(range(s=3, e=5) == [3,4,5]);
|
||||
assert(range(s=3, e=8, step=2) == [3,5,7]);
|
||||
assert(range(s=4, e=8, step=2) == [4,6,8]);
|
||||
assert(range(e=4, n=3) == [0,2,4]);
|
||||
assert(range(n=4, s=[3,4], step=[2,3]) == [[3,4], [5,7], [7,10], [9,13]]);
|
||||
}
|
||||
test_list_range();
|
||||
test_range();
|
||||
|
||||
|
||||
module test_rangex() {
|
||||
assert_equal(rangex(4), [0,1,2,3]);
|
||||
assert_approx(rangex(5,e=1), [0, 0.2, 0.4, 0.6, 0.8]);
|
||||
assert_equal(rangex(n=4, step=2), [0,2,4,6]);
|
||||
assert_approx(rangex(n=4, step=0.25), [0,0.25,0.5,0.75]);
|
||||
assert_equal(rangex(n=4, s=3, step=3), [3,6,9,12]);
|
||||
assert_equal(rangex(e=3), [0,1,2]);
|
||||
assert_equal(rangex(e=6, step=2), [0,2,4]);
|
||||
assert_equal(rangex(s=3, e=5), [3,4]);
|
||||
assert_equal(rangex(s=3, e=8, step=2), [3,5,7]);
|
||||
assert_equal(rangex(s=4, e=8, step=2), [4,6]);
|
||||
assert_equal(rangex(e=6, n=3), [0,2,4]);
|
||||
assert_equal(rangex(n=4, s=[3,4], step=[2,3]), [[3,4], [5,7], [7,10], [9,13]]);
|
||||
}
|
||||
test_rangex();
|
||||
|
||||
|
||||
module test_reverse() {
|
||||
@@ -303,7 +322,7 @@ test_enumerate();
|
||||
|
||||
|
||||
module test_shuffle() {
|
||||
nums1 = [for (i=list_range(100)) i];
|
||||
nums1 = [for (i=range(100)) i];
|
||||
nums2 = shuffle(nums1,33);
|
||||
nums3 = shuffle(nums2,99);
|
||||
assert(sort(nums2)==nums1);
|
||||
|
@@ -481,7 +481,7 @@ module test_circle_3points() {
|
||||
radii = rands(10,100,count,seed_value=390);
|
||||
angles = rands(0,360,count,seed_value=699);
|
||||
// 2D tests.
|
||||
for(i = list_range(count)) {
|
||||
for(i = range(count)) {
|
||||
cp = select(coords,i,i+1);
|
||||
r = radii[i];
|
||||
angs = sort(select(angles,i,i+2));
|
||||
@@ -506,7 +506,7 @@ module test_circle_3points() {
|
||||
assert(approx(res[2], UP));
|
||||
}
|
||||
}
|
||||
for(i = list_range(count)) {
|
||||
for(i = range(count)) {
|
||||
cp = select(coords,i,i+1);
|
||||
r = radii[i];
|
||||
angs = sort(select(angles,i,i+2));
|
||||
@@ -532,7 +532,7 @@ module test_circle_3points() {
|
||||
}
|
||||
}
|
||||
// 3D tests.
|
||||
for(i = list_range(count)) {
|
||||
for(i = range(count)) {
|
||||
cp = select(coords,i,i+2);
|
||||
r = radii[i];
|
||||
nrm = unit(select(coords,i+10,i+12));
|
||||
@@ -559,7 +559,7 @@ module test_circle_3points() {
|
||||
assert(approx(res[2], n));
|
||||
}
|
||||
}
|
||||
for(i = list_range(count)) {
|
||||
for(i = range(count)) {
|
||||
cp = select(coords,i,i+2);
|
||||
r = radii[i];
|
||||
nrm = unit(select(coords,i+10,i+12));
|
||||
@@ -984,8 +984,8 @@ module test_pointlist_bounds() {
|
||||
|
||||
|
||||
module test_closest_point() {
|
||||
ptlist = [for (i=list_range(100)) rands(-100,100,2,seed_value=8463)];
|
||||
testpts = [for (i=list_range(100)) rands(-100,100,2,seed_value=6834)];
|
||||
ptlist = [for (i=range(100)) rands(-100,100,2,seed_value=8463)];
|
||||
testpts = [for (i=range(100)) rands(-100,100,2,seed_value=6834)];
|
||||
for (pt = testpts) {
|
||||
pidx = closest_point(pt,ptlist);
|
||||
dists = [for (p=ptlist) norm(pt-p)];
|
||||
@@ -997,8 +997,8 @@ module test_closest_point() {
|
||||
|
||||
|
||||
module test_furthest_point() {
|
||||
ptlist = [for (i=list_range(100)) rands(-100,100,2,seed_value=8463)];
|
||||
testpts = [for (i=list_range(100)) rands(-100,100,2,seed_value=6834)];
|
||||
ptlist = [for (i=range(100)) rands(-100,100,2,seed_value=8463)];
|
||||
testpts = [for (i=range(100)) rands(-100,100,2,seed_value=6834)];
|
||||
for (pt = testpts) {
|
||||
pidx = furthest_point(pt,ptlist);
|
||||
dists = [for (p=ptlist) norm(pt-p)];
|
||||
|
@@ -1122,7 +1122,7 @@ module test_real_roots(){
|
||||
// Wilkinson polynomial is a nasty test:
|
||||
assert_approx(
|
||||
sort(real_roots(poly_mult([[1,-1],[1,-2],[1,-3],[1,-4],[1,-5],[1,-6],[1,-7],[1,-8],[1,-9],[1,-10]]))),
|
||||
list_range(n=10,s=1));
|
||||
range(n=10,s=1));
|
||||
assert_equal(real_roots([3]), []);
|
||||
assert_equal(real_roots(poly_mult([[1,-2,5],[12,-24,24],[-2, -12, -20],[1,-10,50]])),[]);
|
||||
assert_equal(real_roots(poly_mult([[1,-2,5],[12,-24,24],[-2, -12, -20],[1,-10,50],[1,0,0]])),[0,0]);
|
||||
|
Reference in New Issue
Block a user