aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/test/basic_SUITE_data/basic_pattern_match.erl
blob: 93240354a73c484a0d9a8cbae9c47498d3a93c87 (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
%%% -*- erlang-indent-level: 2 -*-
%%%-------------------------------------------------------------------
%%% Author: Kostis Sagonas
%%%
%%% Contains code examples that test pattern matching against terms of
%%% various types.
%%%-------------------------------------------------------------------
-module(basic_pattern_match).

-export([test/0]).

test() ->
  ok = test_hello_world(),
  ok = test_list_plus_plus_match(),
  ok.

%%--------------------------------------------------------------------
%% Trivial test to test pattern matching compilation with atoms, the
%% correct handling of all sorts of alphanumeric types in Erlang, and
%% conversions between them.

test_hello_world() ->
  String = gimme(string),
  String = atom_to_list(gimme(atom)),
  String = binary_to_list(gimme(binary)),
  true = (list_to_atom(String) =:= gimme(atom)),
  true = (list_to_binary(String) =:= gimme(binary)),
  ok.

gimme(string) ->
  "hello world";
gimme(atom) ->
  'hello world';
gimme(binary) ->
  <<"hello world">>.

%%--------------------------------------------------------------------
%% Makes sure that pattern matching expressions involving ++ work OK.
%% The third expression caused a problem in the Erlang shell of R11B-5.
%% It worked OK in both interpreted and compiled code.

test_list_plus_plus_match() ->
  ok = (fun("X" ++ _) -> ok end)("X"),
  ok = (fun([$X | _]) -> ok end)("X"),
  ok = (fun([$X] ++ _) -> ok end)("X"),
  ok.