1#!./parrot
2# Copyright (C) 2001-2009, Parrot Foundation.
3
4=head1 NAME
5
6t/op/globals.t - Global Variables
7
8=head1 SYNOPSIS
9
10        % prove t/op/globals.t
11
12=head1 DESCRIPTION
13
14Tests the C<get_global> and C<set_global> operations.
15
16=cut
17
18.const int TESTS = 12
19
20.namespace []
21
22.sub 'test' :main
23    .include 'test_more.pir'
24
25    plan(TESTS)
26
27    root_integer()
28    get_null_global()
29    not_found_null()
30    get_set_global_with_key()
31    fetch_and_store()
32    find_null_global()
33    get_hll_global_not_found()
34    find_store_with_key()
35.end
36
37.namespace []
38.sub 'root_integer'
39    $P0 = new ['Integer']
40    $P1 = new ['Integer']
41
42    $P0 = 12
43    $P1 = 7
44
45    set_global "Integer", $P0
46    $P1 = get_global "Integer"
47
48    is($P1, 12, 'root_integer')
49.end
50
51.sub 'get_null_global'
52    $S0 = null
53
54    $I0 = 0
55    push_eh get_null_global_catch
56    $P1 = get_global $S0
57    pop_eh
58    goto get_null_global_end
59
60  get_null_global_catch:
61    $I0 = 1
62
63  get_null_global_end:
64    ok($I0, 'get null global')
65.end
66
67.sub 'not_found_null'
68    $P1 = get_global 'no_such_global'
69    ok(1, 'not_found_null get_global returns')
70
71    $I0 = defined $P1
72    nok($I0, 'not_found_null get_global returns null')
73.end
74
75.sub 'get_set_global_with_key'
76    $P0 = get_global ['Monkey'], 'do_explosion'
77    $I0 = defined $P0
78    ok($I0, 'get_global of a sub with a key')
79    $P0()
80.end
81
82.namespace ['Monkey']
83.sub 'do_explosion'
84    set_it()
85    $P1 = get_hll_global [ "Monkey" ; "Toaster" ], "Explosion"
86    is($P1, "Ook...BANG!\n", 'get/set_global with key')
87.end
88
89.sub 'set_it'
90        $P0 = new 'String'
91        $P0 = "Ook...BANG!\n"
92        set_global [ "Toaster" ], "Explosion", $P0
93.end
94
95#----------------------------------------------------------------
96# LEGACY
97#----------------------------------------------------------------
98
99.namespace []
100
101.sub 'fetch_and_store'
102    $P0 = new 'Integer'
103    $P1 = new 'Integer'
104
105    $P0 = 12
106    $P1 = 7
107
108    set_global 'Integer', $P0
109    $P1 = get_hll_global 'Integer'
110    is($P1, 12, 'Fetch and store')
111.end
112
113.sub 'find_null_global'
114    $S0 = null
115    push_eh find_null_global_catch
116    $P1 = get_hll_global $S0
117    pop_eh
118    $I0 = 0
119    goto find_null_global_end
120
121  find_null_global_catch:
122    $I0 = 1
123  find_null_global_end:
124    ok($I0, 'Find null global')
125.end
126
127.sub 'get_hll_global_not_found'
128    $P1 = get_hll_global 'no such global'
129    ok(1, 'get_hll_global returns if not found')
130
131    $I0 = defined $P1
132    nok($I0, 'get_hll_global returns null if not found')
133.end
134
135.sub 'find_store_with_key'
136    $P0 = get_global ['Monkey2'], 'do_explosion'
137    $I0 = defined $P0
138    ok($I0, 'get_hll_global of a sub with a key')
139    $P0()
140.end
141
142.namespace ['Monkey2']
143.sub 'do_explosion'
144    set_it()
145    $P1 = get_hll_global [ "Monkey2" ; "Toaster" ], "Explosion"
146    is($P1, "Ook...BANG, again!\n", 'find/store global with key')
147.end
148
149.sub 'set_it'
150    $P0 = new 'String'
151    $P0 = "Ook...BANG, again!\n"
152    set_hll_global [ "Monkey2"; "Toaster" ], "Explosion", $P0
153.end
154
155# Local Variables:
156#   mode: pir
157#   fill-column: 100
158# End:
159# vim: expandtab shiftwidth=4 ft=pir:
160