tests: use new array init syntax, fix mutability

pull/4606/head
Alexander Medvednikov 2020-04-26 11:56:27 +02:00
parent 0641a31fe0
commit 153ac230ec
7 changed files with 23 additions and 17 deletions

View File

@ -22,7 +22,7 @@ fn test_pointer() {
} }
fn test_assign() { fn test_assign() {
arr := [2, 4, 8, 16, 32, 64, 128] mut arr := [2, 4, 8, 16, 32, 64, 128]
arr[0] = 2 arr[0] = 2
arr[1] &= 255 arr[1] &= 255
@ -99,7 +99,7 @@ struct K {
} }
fn test_empty() { fn test_empty() {
mut chunks := []Chunk mut chunks := []Chunk{}
a := Chunk{} a := Chunk{}
assert chunks.len == 0 assert chunks.len == 0
chunks << a chunks << a
@ -111,7 +111,7 @@ fn test_empty() {
} }
fn test_push() { fn test_push() {
mut a := []int mut a := []int{}
a << 1 a << 1
a << 3 a << 3
assert a[1] == 3 assert a[1] == 3
@ -289,7 +289,7 @@ fn test_reverse() {
for i, _ in d { for i, _ in d {
assert d[i] == b[b.len - i - 1] assert d[i] == b[b.len - i - 1]
} }
e := []int e := []int{}
f := e.reverse() f := e.reverse()
assert f.len == 0 assert f.len == 0
} }
@ -711,7 +711,7 @@ fn test_hex(){
} }
fn test_left_shift_precendence() { fn test_left_shift_precendence() {
mut arr := []int mut arr := []int{}
arr << 1 + 1 arr << 1 + 1
arr << 1 - 1 arr << 1 - 1
arr << 2 / 1 arr << 2 / 1

View File

@ -11,7 +11,7 @@ const (
// NOTE: temp until we have []bytes(buff) // NOTE: temp until we have []bytes(buff)
fn c_array_to_bytes_tmp(len int, buffer voidptr) []byte { fn c_array_to_bytes_tmp(len int, buffer voidptr) []byte {
mut arr := []byte mut arr := []byte{}
arr = make(len, 1, 1) arr = make(len, 1, 1)
arr.data = buffer arr.data = buffer
/* /*

View File

@ -31,7 +31,7 @@ fn test_crypto_rand_read() {
fn test_crypto_rand_int_u64() { fn test_crypto_rand_int_u64() {
max := u64(160) max := u64(160)
mut unique := []int mut unique := []int{}
for _ in 0..80 { for _ in 0..80 {
r := rand.int_u64(max) or { r := rand.int_u64(max) or {
assert false assert false

View File

@ -4,10 +4,8 @@
module rand module rand
import( import math.bits
math.bits import encoding.binary
encoding.binary
)
pub fn int_u64(max u64) ?u64 { pub fn int_u64(max u64) ?u64 {
bitlen := bits.len_64(max) bitlen := bits.len_64(max)

View File

@ -16,7 +16,7 @@ pub fn next(max int) int {
// rand_r returns a pseudo-random number; // rand_r returns a pseudo-random number;
// writes a result value to the seed argument. // writes a result value to the seed argument.
pub fn rand_r(seed mut int) int { pub fn rand_r(seed &int) int {
ns := *seed * 1103515245 + 12345 ns := *seed * 1103515245 + 12345
(*seed) = ns (*seed) = ns
return ns & 0x7fffffff return ns & 0x7fffffff

View File

@ -5,21 +5,26 @@ struct Test {
} }
fn test_interpolation_map_to_string() { fn test_interpolation_map_to_string() {
a := map[string]string if true {
}
//
else{}
mut a := map[string]string
a['1'] = 'one' a['1'] = 'one'
a['2'] = 'two' a['2'] = 'two'
a['3'] = 'three' a['3'] = 'three'
assert '$a' == 'map_string_string{1: one, 2: two, 3: three}' assert '$a' == 'map_string_string{1: one, 2: two, 3: three}'
b := map[string]int mut b := map[string]int
b['1'] = 1 b['1'] = 1
b['2'] = 2 b['2'] = 2
b['3'] = 3 b['3'] = 3
assert '$b' == 'map_string_int{1: 1, 2: 2, 3: 3}' assert '$b' == 'map_string_int{1: 1, 2: 2, 3: 3}'
c := map[string]bool mut c := map[string]bool
c['1'] = true c['1'] = true
c['2'] = false c['2'] = false
assert '$c' == 'map_string_bool{1: true, 2: false}' assert '$c' == 'map_string_bool{1: true, 2: false}'
d := map[string]Test mut d := map[string]Test
d['1'] = Test{true, 0, 'abc'} d['1'] = Test{true, 0, 'abc'}
d['2'] = Test{true, 1, 'def'} d['2'] = Test{true, 1, 'def'}
d['3'] = Test{false, 2, 'ghi'} d['3'] = Test{false, 2, 'ghi'}

View File

@ -1,5 +1,8 @@
fn test_nested_maps() { fn test_nested_maps() {
x := map[string]map[string]int if true{}
//
else{}
mut x := map[string]map[string]int
x["a"] = map[string]int x["a"] = map[string]int
assert x["a"]["b"] == 0 assert x["a"]["b"] == 0
x["a"]["b"] = 5 x["a"]["b"] = 5