1// ============================================================================
2// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3// Copyright (C) 2014 - Scilab Enterprises - Simon MARCHETTO
4//
5//  This file is distributed under the same license as the Scilab package.
6// ============================================================================
7//
8// <-- CLI SHELL MODE -->
9function generate_error(a, b, msg)
10    disp(typeof(a));
11    disp(a);
12    if isequal(a, b) then
13        disp(" == ");
14    else
15         disp(" <> ");
16    end
17    disp(typeof(b));
18    disp(b);
19    assert_generror(msg);
20end
21function check_inter_type(type1_values, type2_values)
22    if isequal(type1_values(2), type2_values(2)) then
23        msg = msprintf("check_inter_type fails: values %s(%d) and %s(%d) must not be equal !", type1_values(1), 2, type2_values(1), 2);
24        generate_error(type1_values(2), type2_values(2), msg);
25    end
26end
27function check_intra_type(type_values)
28    for i=2:size(type_values)
29        for j=i:size(type_values)
30            if i <> j then
31                if isequal(type_values(i), type_values(j)) then
32                    msg = msprintf("check_intra_type fails: values %s(%d) and %s(%d) must not be equal !", ..
33                      type_values(1), i, type_values(1), j);
34                    generate_error(type_values(i), type_values(j), msg);
35                end
36            else
37                if ~isequal(type_values(i), type_values(j)) then
38                    msg = msprintf("check_intra_type fails: values %s(%d) and %s(%d) must be equal !", ..
39                      type_values(1), i, type_values(1), j);
40                    generate_error(type_values(i), type_values(j), msg);
41                end
42            end
43        end
44    end
45end
46function check_types(types)
47    for i=1:size(types)
48        for j=i+1:size(types)
49            check_inter_type(types(i), types(j));
50        end
51        check_intra_type(types(i));
52    end
53end
54// "constant" type
55real_scalars = list(1, -1, 3.2, 3.2000005, %pi, %e, %inf, -%inf, %eps, []);
56real_matrices = list([1.1, 2.2], [1.1 2.2; 3.3 4.4], [1.1 2.2; 3.3 4.4; 5.5 6.6], [1.1 2.2 3.3; 4.4 5.5 6.6]);
57reals = lstcat(real_scalars, real_matrices);
58// TODO: check %nan is not equal to anything
59complex_scalars = list(%i, 3.2*%i, -%i, 1+0.5*%i);
60complex_matrices = list([1+%i, 2+2*%i], [1+%i 2+2*%i; 3+3*%i 4+4*%i], ..
61    [1+%i 2+2*%i; 3+3*%i 4+4*%i; 5+5*%i 6+6*%i], [1+%i 2+2*%i 3+3*%i; 4+4*%i 5+5*%i 6+6*%i]);
62complexs = lstcat(complex_scalars, complex_matrices);
63constants = lstcat(reals, complexs);
64constants(0) = 'constants';
65// "polynomial" type
66polynoms = list(%s, -%s, %s+1, 1+%s+%s^2, 1+%i+%s-%s^2, 1+%s+%i*%s^3);
67polynoms(0) = 'polynoms';
68// boolean type
69bool_scalars = list(%t, %f);
70bool_matrices = list([%t, %f], [%t %f; %t %f], [%t %f; %t %f; %t %f], [%t %f %t; %t %f %t]);
71bools = lstcat(bool_scalars, bool_matrices);
72bools(0) = 'bools';
73// "sparse" type
74sparses = list(sparse([1]), sparse([0]), sparse([0 0]), sparse([0 0; 0 0]), sparse([1 2]), ..
75    sparse([1 2; 3 4]), sparse([-1 2; 3 4]), sparse([1 2; 3 -1]), sparse([1 2; 3 0]), sparse([0 2; 3 4]), sparse([0 1; 2 3]));
76sparses(0) = 'sparses';
77// "boolean sparse" type
78boolean_sparses = list(sparse([]), sparse([%t]), sparse([%f]), sparse([%f %f]), sparse([%f %f; %f %f]), sparse([%t %t]), ..
79    sparse([%t %t; %t %t]), sparse([%f %t; %t %t]), sparse([%t %t; %t %f]));
80boolean_sparses(0) = 'boolean_sparses';
81// integer types
82int8s_scalars = list(int8(1), int8(-1));
83int8s_matrices = list(int8([1, 2]), int8([1 2; 3 4]), int8([1 2; 3 4; 5 6]), int8([1 2 3; 4 5 6]));
84int8s = lstcat(int8s_scalars, int8s_matrices);
85function int_type_values = convert_to_int_type(type_values, int_type)
86    int_type_values = list();
87    for i=1:size(type_values)
88        int_type_values($+1) = iconvert(type_values(i), int_type);
89    end
90end
91uint8s = convert_to_int_type(int8s, 11);
92int16s = convert_to_int_type(int8s, 2);
93uint16s = convert_to_int_type(int8s, 12);
94int32s = convert_to_int_type(int8s, 4);
95uint32s = convert_to_int_type(int8s, 14);
96int64s = convert_to_int_type(int8s, 8);
97uint64s = convert_to_int_type(int8s, 18);
98ints = lstcat(int8s, uint8s, int16s, uint16s, int32s, uint32s, int64s, uint64s);
99ints(0) = 'ints';
100// "handle" type
101// "string" type
102string_scalars = list("", " ", "a", "A", "à", ascii(13), "aaaa", "aaab", "aaaa" + ascii(13));
103string_matrices = list(["", ""], [" ", " "], ["a", "a"], ["a", "A"], ["a", "à"], ..
104    ["a" "b"; "c" "d"], ["a" "b"; "c" "d"; "e" "f"], ["a" "b" "c"; "d" "e" "f"]);
105strings = lstcat(string_scalars, string_matrices);
106strings(0) = 'strings';
107// "function" type
108function funcNoParamNoBodyNoReturn(); end;
109function funcWithParamNoBodyNoReturn(x); end;
110function funcNoParamWithBodyNoReturn(); disp("Hello"); end;
111function funcWithParamWithBodyNoReturn(x); disp(x); end;
112function y = funcNoParamWithReturn(); y = 1; end;
113function y = funcWithParamWithReturn(x); y = x; end;
114functions = list(funcNoParamNoBodyNoReturn, ..
115    funcWithParamNoBodyNoReturn, ..
116    funcNoParamWithBodyNoReturn, ..
117    funcWithParamWithBodyNoReturn, ..
118    funcNoParamWithReturn, ..
119    funcWithParamWithReturn);
120functions(0) = 'functions';
121// "fptr" type
122fptrs = list(cos, sin);
123fptrs(0) = 'fptrs';
124// "list" type
125lists = list(list(), list(1), list("a"), list(1, 2), list(list(1)), list(list(2)), list(list(1, 2)));
126lists(0) = 'lists';
127// "tlist" type
128tlists = list(tlist(["t1"]), tlist(["t2"]), tlist(["t1", "f1"], "a"), tlist(["t1", "f2"], "a"), ..
129    tlist(["t1", "f1", "f2"], "a", "b"), tlist(["t1", "f1"], "b"));
130tlists(0) = 'tlists';
131// "mlist" type
132mlists = list(mlist(["m1"]), mlist(["m2"]), mlist(["m1", "f1"], "a"), mlist(["m1", "f2"], "a"), ..
133    mlist(["m1", "f1", "f2"], "a", "b"), mlist(["m1", "f1"], "b"));
134mlists(0) = 'mlists';
135// "st" type
136structs = list(struct("a", 1), struct("a", 2), struct("b", 1), struct("a", 1, "b", 2));
137structs(0) = 'structs';
138// "pointer" type
139// TODO: add functions, fptrs, handles (when operator== will be fixed in that types)
140types = list(constants, polynoms, bools, sparses, boolean_sparses, ints, strings, lists, tlists, mlists, structs);
141check_types(types);
142