aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/test/basic_SUITE_data/basic_comparisons.erl
blob: 8dab2cab1f28b72fa0c53d5a1d1a121c55cf3eaa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
%%% -*- erlang-indent-level: 2 -*-
%%%-------------------------------------------------------------------
%%% Author: Kostis Sagonas
%%%
%%% Contains tests for correct execution of comparison operators.
%%%-------------------------------------------------------------------
-module(basic_comparisons).

-export([test/0]).

test() ->
  Ns = [0, 0.0, 42, 42.0, gazonk],
  T1F4 = [true, false, false, false, false],
  T2F3 = [true, true, false, false, false],
  F1T4 = [false, true, true, true, true],
  F2T3 = [false, false, true, true, true],
  %% tests for calls
  T1F4 = [eq_exact_call(0, N) || N <- Ns],
  F1T4 = [ne_exact_call(0, N) || N <- Ns],
  T2F3 = [eq_call(0, N) || N <- Ns],
  F2T3 = [ne_call(0, N) || N <- Ns],
  %% tests for guards
  T1F4 = [eq_exact_guard(0, N) || N <- Ns],
  F1T4 = [ne_exact_guard(0, N) || N <- Ns],
  T2F3 = [eq_guard(0, N) || N <- Ns],
  F2T3 = [ne_guard(0, N) || N <- Ns],
  %% some more tests
  ok = test_against_zero(),
  ok = test_against_other_terms(),
  ok = test_sofs_func(),
  ok.

test_against_zero() ->
  Xs = [0, 1, 0.0],
  [true, false, false] = [is_zero_int(X) || X <- Xs],
  [true, false,  true] = [is_zero_num(X) || X <- Xs],
  [false, true,  true] = [is_nonzero_int(X) || X <- Xs],
  [false, true, false] = [is_nonzero_num(X) || X <- Xs],
  ok.

test_against_other_terms() ->
  TTT = {true, true, true},
  FFF = {false, false, false},
  TTT = {is_foo_exact(foo), is_foo_term1(foo), is_foo_term2(foo)},
  FFF = {is_foo_exact(bar), is_foo_term1(bar), is_foo_term2(bar)},
  FFF = {is_nonfoo_exact(foo), is_nonfoo_term1(foo), is_nonfoo_term2(foo)},
  TTT = {is_nonfoo_exact(bar), is_nonfoo_term1(bar), is_nonfoo_term2(bar)},
  Tup = {a, {42}, [c]},
  TTT = {is_tuple_skel(Tup), is_tuple_exact(Tup), is_tuple_term(Tup)},
  BNi = <<42>>,
  TTT = {is_bin_exact(BNi), is_bin_term1(BNi), is_bin_term2(BNi)},
  BNf = <<42/float>>,
  FFF = {is_bin_exact(BNf), is_bin_term1(BNf), is_bin_term2(BNf)},
  ok.

test_sofs_func() ->
  L = [0, 0.0],
  ok = sofs_func(L, L, L).

%%--------------------------------------------------------------------
%% Test for comparison operators used in body calls

eq_exact_call(X, Y) -> X =:= Y.

ne_exact_call(X, Y) -> X =/= Y.

eq_call(X, Y) -> X == Y.

ne_call(X, Y) -> X /= Y.

%%--------------------------------------------------------------------
%% Tests for comparison operators used as guards

eq_exact_guard(X, Y) when X =:= Y -> true;
eq_exact_guard(_, _) -> false.

ne_exact_guard(X, Y) when X =/= Y -> true;
ne_exact_guard(_, _) -> false.

eq_guard(X, Y) when X == Y -> true;
eq_guard(_, _) -> false.

ne_guard(X, Y) when X /= Y -> true;
ne_guard(_, _) -> false.

%%--------------------------------------------------------------------

is_zero_int(N) when N =:= 0 -> true;
is_zero_int(_) -> false.

is_nonzero_int(N) when N =/= 0 -> true;
is_nonzero_int(_) -> false.

is_zero_num(N) when N == 0 -> true;
is_zero_num(_) -> false.

is_nonzero_num(N) when N /= 0 -> true;
is_nonzero_num(_) -> false.

%%--------------------------------------------------------------------
%% There should not really be any difference in the generated code
%% for the following three functions.

is_foo_exact(A) when A =:= foo -> true;
is_foo_exact(_) -> false.

is_foo_term1(A) when A == foo -> true;
is_foo_term1(_) -> false.

is_foo_term2(A) when foo == A -> true;
is_foo_term2(_) -> false.

%%--------------------------------------------------------------------
%% Same for these cases

is_nonfoo_exact(A) when A =/= foo -> true;
is_nonfoo_exact(_) -> false.

is_nonfoo_term1(A) when A /= foo -> true;
is_nonfoo_term1(_) -> false.

is_nonfoo_term2(A) when foo /= A -> true;
is_nonfoo_term2(_) -> false.

%%--------------------------------------------------------------------

is_tuple_skel({A,{B},[C]}) when is_atom(A), is_integer(B), is_atom(C) -> true;
is_tuple_skel(T) when is_tuple(T) -> false.

is_tuple_exact(T) when T =:= {a,{42},[c]} -> true;
is_tuple_exact(T) when is_tuple(T) -> false.

is_tuple_term(T) when T == {a,{42.0},[c]} -> true;
is_tuple_term(T) when is_tuple(T) -> false.

%%--------------------------------------------------------------------
%% But for binaries the treatment has to be different, due to the need
%% for construction of the binary in the guard.

is_bin_exact(B) when B =:= <<42>> -> true;
is_bin_exact(_) -> false.

is_bin_term1(B) when B == <<42>> -> true;
is_bin_term1(_) -> false.

is_bin_term2(B) when <<42>> == B -> true;
is_bin_term2(_) -> false.

%%--------------------------------------------------------------------
%% a test from sofs.erl which failed at some point

sofs_func([X | Ts], X0, L) when X /= X0 ->
  sofs_func(Ts, X, L);
sofs_func([X | _Ts], X0, _L) when X == X0 ->
  ok;
sofs_func([], _X0, L) ->
  L.