92 lines
4.5 KiB
Go
92 lines
4.5 KiB
Go
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// t_list_test.go
|
|
package expr
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.portale-stac.it/go-pkg/expr/types/array"
|
|
"git.portale-stac.it/go-pkg/expr/types/list"
|
|
)
|
|
|
|
func TestArray(t *testing.T) {
|
|
section := "Array"
|
|
|
|
inputs := []inputType{
|
|
/* 1 */ {`[]`, array.NewArrayA(), nil},
|
|
/* 2 */ {`[1,2,3]`, array.NewArrayA(int64(1), int64(2), int64(3)), nil},
|
|
/* 3 */ {`[1,2,"hello"]`, array.NewArrayA(int64(1), int64(2), "hello"), nil},
|
|
/* 4 */ {`[1+2, not true, "hello"]`, array.NewArrayA(int64(3), false, "hello"), nil},
|
|
/* 5 */ {`[1,2]+[3]`, array.NewArrayA(int64(1), int64(2), int64(3)), nil},
|
|
/* 6 */ {`[1,4,3,2]-[3]`, array.NewArrayA(int64(1), int64(4), int64(2)), nil},
|
|
/* 7 */ {`builtin "math.arith"; add([1,4,3,2])`, int64(10), nil},
|
|
/* 8 */ {`builtin "math.arith"; add([1,[2,2],3,2])`, int64(10), nil},
|
|
/* 9 */ {`builtin "math.arith"; mul([1,4,3.0,2])`, float64(24.0), nil},
|
|
/* 10 */ {`builtin "math.arith"; add([1,"hello"])`, nil, `add(): param nr 2 (2 in 1) has wrong type string, number expected`},
|
|
/* 11 */ {`[a=1,b=2,c=3] but a+b+c`, int64(6), nil},
|
|
/* 12 */ {`[1,2,3] <+ 2+2`, array.NewArrayA(int64(1), int64(2), int64(3), int64(4)), nil},
|
|
/* 13 */ {`2-1 +> [2,3]`, array.NewArrayA(int64(1), int64(2), int64(3)), nil},
|
|
/* 14 */ {`[1,2,3][1]`, int64(2), nil},
|
|
/* 15 */ {`ls=[1,2,3] but ls[1]`, int64(2), nil},
|
|
/* 16 */ {`ls=[1,2,3] but ls[-1]`, int64(3), nil},
|
|
/* 17 */ {`list=["one","two","three"]; list[10]`, nil, `[1:34] index 10 out of bounds`},
|
|
/* 18 */ {`["a", "b", "c"]`, array.NewArrayA("a", "b", "c"), nil},
|
|
/* 19 */ {`["a", "b", "c"]`, array.NewArray([]any{"a", "b", "c"}), nil},
|
|
/* 20 */ {`#["a", "b", "c"]`, int64(3), nil},
|
|
/* 21 */ {`"b" in ["a", "b", "c"]`, true, nil},
|
|
/* 22 */ {`a=[1,2]; (a)<+3`, array.NewArrayA(int64(1), int64(2), int64(3)), nil},
|
|
/* 23 */ {`a=[1,2]; (a)<+3; a`, array.NewArrayA(int64(1), int64(2)), nil},
|
|
/* 24 */ {`["a","b","c","d"][1]`, "b", nil},
|
|
/* 25 */ {`["a","b","c","d"][1,1]`, nil, `[1:19] one index only is allowed`},
|
|
/* 26 */ {`[0,1,2,3,4][:]`, array.NewArrayA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
|
|
/* 27 */ {`["a", "b", "c"] <+ ;`, nil, `[1:18] infix operator "<+" requires two non-nil operands, got 1`},
|
|
/* 28 */ {`2 << 3;`, int64(16), nil},
|
|
/* 29 */ {`but +> ["a", "b", "c"]`, nil, `[1:6] infix operator "+>" requires two non-nil operands, got 0`},
|
|
/* 30 */ {`2 >> 3;`, int64(0), nil},
|
|
/* 31 */ {`a=[1,2]; a<+3`, array.NewArrayA(int64(1), int64(2), int64(3)), nil},
|
|
/* 32 */ {`a=[1,2]; 5+>a`, array.NewArrayA(int64(5), int64(1), int64(2)), nil},
|
|
/* 33 */ {`L=[1,2]; L[0]=9; L`, array.NewArrayA(int64(9), int64(2)), nil},
|
|
/* 34 */ {`L=[1,2]; L[5]=9; L`, nil, `index 5 out of bounds (0, 1)`},
|
|
/* 35 */ {`L=[1,2]; L[]=9; L`, nil, `[1:12] index/key specification expected, got [] [array]`},
|
|
/* 36 */ {`L=[1,2]; L[nil]=9;`, nil, `[1:12] index/key is nil`},
|
|
/* 37 */ {`[0,1,2,3,4][2:3]`, array.NewArrayA(int64(2)), nil},
|
|
/* 38 */ {`[0,1,2,3,4][3:-1]`, array.NewArrayA(int64(3)), nil},
|
|
/* 30 */ {`[0,1,2,3,4][-3:-1]`, array.NewArrayA(int64(2), int64(3)), nil},
|
|
/* 40 */ {`[0,1,2,3,4][0:]`, array.NewArrayA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
|
|
/* 41 */ {`[0] << $([1,2,3,4])`, array.NewArrayA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
|
|
/* 42 */ {`L=[]; [1] >> L; L`, array.NewArrayA(), nil},
|
|
/* 43 */ {`L=[]; L << [1]; L`, array.NewArrayA(), nil},
|
|
// /* 44 */ {`[0,1,2,3,4][2:3]`, kern.NewListA(int64(20)), nil},
|
|
}
|
|
|
|
// t.Setenv("EXPR_PATH", ".")
|
|
|
|
// runTestSuiteSpec(t, section, inputs, 44)
|
|
RunTestSuite(t, section, inputs)
|
|
}
|
|
|
|
func TestLinkedListParser(t *testing.T) {
|
|
section := "Linked-List"
|
|
|
|
inputs := []inputType{
|
|
/* 1 */ {`[<1,2>]`, list.NewLinkedListA(1, 2), nil},
|
|
/* 2 */ {`it=$([<1,2,3>]); it++`, int64(1), nil},
|
|
/* 3 */ {`it=$([<1,2,3>]); #($$(it))`, int64(3), nil},
|
|
/* 4 */ {`[<1,2,3>][0]`, int64(1), nil},
|
|
/* 5 */ {`[<1,2,3>][0:2]`, list.NewLinkedListA(1, 2), nil},
|
|
/* 6 */ {`[<1>] + [<2,3>]`, list.NewLinkedListA(1, 2, 3), nil},
|
|
/* 7 */ {`L=[<1>] + [<2,3>]; L[0]=100; L`, list.NewLinkedListA(100, 2, 3), nil},
|
|
/* 8 */ {`builtin "base"; L=[<1>]; isList(L)`, true, nil},
|
|
/* 9 */ {`2 IN [<1,2,3>]`, true, nil},
|
|
/* 10 */ {`2 AT [<1,2,3>]`, int64(1), nil},
|
|
/* 11 */ {`4 AT [<1,2,3>]`, int64(-1), nil},
|
|
}
|
|
|
|
// t.Setenv("EXPR_PATH", ".")
|
|
|
|
// runTestSuiteSpec(t, section, inputs, 4)
|
|
RunTestSuite(t, section, inputs)
|
|
}
|