fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Fabian Boehmbuiltin random: Be less strict about argumentsb2e660936

master
3.0 KiB111 linesraw
1#RUN: %fish %s
2set -l max 9223372036854775807
3set -l close_max 9223372036854775806
4set -l min -9223372036854775807
5set -l close_min -9223372036854775806
6set -l diff_max 18446744073709551614
7
8# check failure cases
9random a
10#CHECKERR: random: a: invalid integer
11random $diff_max
12#CHECKERR: random: 18446744073709551614: invalid integer
13random -- 1 2 3 4
14#CHECKERR: random: too many arguments
15random -- 10 $diff_max
16#CHECKERR: random: 18446744073709551614: invalid integer
17random -- 1 1d
18random -- 1 1c 10
19#CHECKERR: random: 1d: invalid integer
20#CHECKERR: random: 1c: invalid integer
21random -- 1 - 10
22#CHECKERR: random: -: invalid integer
23random -- 1 -1 10
24#CHECKERR: random: -1: invalid integer
25random -- 1 $min 10
26#CHECKERR: random: -9223372036854775807: invalid integer
27random -- 1 0 10
28#CHECKERR: random: STEP must be a positive integer
29random choice
30#CHECKERR: random: nothing to choose from
31random choic a b c
32#CHECKERR: random: too many arguments
33
34function check_boundaries
35    if not test "$argv[1]" -ge "$argv[2]" -a "$argv[1]" -le "$argv[3]"
36        printf "Unexpected: %s <= %s <= %s not verified\n" $argv[2] $argv[1] $argv[3] >&2
37        status print-stack-trace
38        return 1
39    end
40end
41
42function test_range
43    return (check_boundaries (random -- $argv) $argv)
44end
45
46function check_contains
47    if not contains -- $argv[1] $argv[2..-1]
48        printf "Unexpected: %s not among possibilities" $argv[1] >&2
49        printf " %s" $argv[2..-1] >&2
50        printf "\n" >&2
51        status print-stack-trace
52        return 1
53    end
54end
55
56function test_step
57    return (check_contains (random -- $argv) (seq -- $argv))
58end
59
60function test_choice
61    return (check_contains (random choice $argv) $argv)
62end
63
64for i in (seq 10)
65    check_boundaries (random) 0 32767
66
67    test_range 0 10
68    test_range -10 -1
69    test_range -10 10
70    test_range 5 5
71
72    test_range 0 $max
73    test_range $min -1
74    test_range $min $max
75
76    test_range $close_max $max
77    test_range $min $close_min
78    test_range $close_min $close_max
79
80    #OSX's `seq` uses scientific notation for large numbers, hence not usable here
81    check_contains (random -- 0 $max $max) 0 $max
82    check_contains (random -- 0 $close_max $max) 0 $close_max
83    check_contains (random -- $min $max 0) $min 0
84    check_contains (random -- $min $close_max 0) $min -1
85    check_contains (random -- $min $max $max) $min 0 $max
86    check_contains (random -- $min $diff_max $max) $min $max
87    check_contains (random -- 5 0) 0 1 2 3 4 5
88
89    test_step 0 $i 10
90    test_step -5 $i 5
91    test_step -10 $i 0
92
93    test_choice a
94    test_choice foo bar
95    test_choice bass trout salmon zander perch carp
96end
97
98
99#check seeding
100set -l seed (random)
101random $seed
102set -l run1 (random) (random) (random) (random) (random)
103random $seed
104set -l run2 (random) (random) (random) (random) (random)
105if not test "$run1" = "$run2"
106    printf "Unexpected different sequences after seeding with %s\n" $seed
107    printf "%s " $run1
108    printf "\n"
109    printf "%s " $run2
110    printf "\n"
111end