1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

rand: simplify rand.PRNG, move to optional types for error handling (#13570)

This commit is contained in:
Subhomoy Haldar
2022-02-23 16:06:14 +05:30
committed by GitHub
parent 5c0b7b0d05
commit 114a341f5f
49 changed files with 609 additions and 1586 deletions

View File

@@ -36,10 +36,10 @@ fn test_bf_and_not_or_xor() {
mut input2 := bitfield.new(len)
mut i := 0
for i < len {
if rand.intn(2) == 1 {
if rand.intn(2) or { 0 } == 1 {
input1.set_bit(i)
}
if rand.intn(2) == 1 {
if rand.intn(2) or { 0 } == 1 {
input2.set_bit(i)
}
i++
@@ -62,7 +62,7 @@ fn test_clone_cmp() {
len := 80
mut input := bitfield.new(len)
for i in 0 .. len {
if rand.intn(2) == 1 {
if rand.intn(2) or { 0 } == 1 {
input.set_bit(i)
}
}
@@ -75,7 +75,7 @@ fn test_slice_join() {
len := 80
mut input := bitfield.new(len)
for i in 0 .. len {
if rand.intn(2) == 1 {
if rand.intn(2) or { 0 } == 1 {
input.set_bit(i)
}
}
@@ -98,7 +98,7 @@ fn test_pop_count() {
mut count0 := 0
mut input := bitfield.new(len)
for i in 0 .. len {
if rand.intn(2) == 1 {
if rand.intn(2) or { 0 } == 1 {
input.set_bit(i)
count0++
}
@@ -113,7 +113,7 @@ fn test_hamming() {
mut input1 := bitfield.new(len)
mut input2 := bitfield.new(len)
for i in 0 .. len {
match rand.intn(4) {
match rand.intn(4) or { 0 } {
0, 1 {
input1.set_bit(i)
count++
@@ -152,7 +152,7 @@ fn test_bf_from_str() {
len := 80
mut input := ''
for _ in 0 .. len {
if rand.intn(2) == 1 {
if rand.intn(2) or { 0 } == 1 {
input = input + '1'
} else {
input = input + '0'
@@ -172,7 +172,7 @@ fn test_bf_bf2str() {
len := 80
mut input := bitfield.new(len)
for i in 0 .. len {
if rand.intn(2) == 1 {
if rand.intn(2) or { 0 } == 1 {
input.set_bit(i)
}
}
@@ -211,7 +211,7 @@ fn test_bf_clear_all() {
len := 80
mut input := bitfield.new(len)
for i in 0 .. len {
if rand.intn(2) == 1 {
if rand.intn(2) or { 0 } == 1 {
input.set_bit(i)
}
}
@@ -229,7 +229,7 @@ fn test_bf_reverse() {
len := 80
mut input := bitfield.new(len)
for i in 0 .. len {
if rand.intn(2) == 1 {
if rand.intn(2) or { 0 } == 1 {
input.set_bit(i)
}
}
@@ -246,9 +246,9 @@ fn test_bf_reverse() {
fn test_bf_resize() {
len := 80
mut input := bitfield.new(rand.intn(len) + 1)
mut input := bitfield.new(rand.intn(len) or { 0 } + 1)
for _ in 0 .. 100 {
input.resize(rand.intn(len) + 1)
input.resize(rand.intn(len) or { 0 } + 1)
input.set_bit(input.get_size() - 1)
}
assert input.get_bit(input.get_size() - 1) == 1
@@ -272,12 +272,12 @@ fn test_bf_pos() {
mut needle := bitfield.new(i)
// fill the needle with random values
for k in 0 .. i {
if rand.intn(2) == 1 {
if rand.intn(2) or { 0 } == 1 {
needle.set_bit(k)
}
}
// make sure the needle contains at least one set bit, selected randomly
r := rand.intn(i)
r := rand.intn(i) or { 0 }
needle.set_bit(r)
// create the haystack, make sure it contains the needle
mut haystack := needle.clone()
@@ -323,7 +323,7 @@ fn test_bf_printing() {
len := 80
mut input := bitfield.new(len)
for i in 0 .. len {
if rand.intn(2) == 0 {
if rand.intn(2) or { 0 } == 0 {
input.set_bit(i)
}
}