1
+ @testset " output" begin
2
+
3
+ @testset " formatted output" begin
4
+ _, out = @grab_output @on_device @mtlprintf (" " )
5
+ @test out == " "
6
+
7
+ _, out = @grab_output @on_device @mtlprintf (" Testing...\n " )
8
+ @test out == " Testing...\n "
9
+
10
+ # narrow integer
11
+ _, out = @grab_output @on_device @mtlprintf (" Testing %d %d...\n " , Int32 (1 ), Int32 (2 ))
12
+ @test out == " Testing 1 2...\n "
13
+
14
+ # wide integer
15
+ _, out = @grab_output @on_device @mtlprintf (" Testing %ld %ld...\n " , Int64 (1 ), Int64 (2 ))
16
+ @test out == " Testing 1 2...\n "
17
+
18
+ _, out = @grab_output @on_device begin
19
+ @mtlprintf (" foo" )
20
+ @mtlprintf (" bar\n " )
21
+ end
22
+ @test out == " foobar\n "
23
+
24
+ # c argument promotions
25
+ function kernel (A)
26
+ @mtlprintf (" %f %f\n " , A[1 ], A[1 ])
27
+ return
28
+ end
29
+ x = mtl (ones (2 , 2 ))
30
+ _, out = @grab_output begin
31
+ Metal. @sync @metal kernel (x)
32
+ end
33
+ @test out == " 1.000000 1.000000\n "
34
+ end
35
+
36
+ @testset " @mtlprint" begin
37
+ # basic @mtlprint/@mtlprintln
38
+
39
+ _, out = @grab_output @on_device @mtlprint (" Hello, World\n " )
40
+ @test out == " Hello, World\n "
41
+
42
+ _, out = @grab_output @on_device @mtlprintln (" Hello, World" )
43
+ @test out == " Hello, World\n "
44
+
45
+
46
+ # argument interpolation (by the macro, so can use literals)
47
+
48
+ _, out = @grab_output @on_device @mtlprint (" foobar" )
49
+ @test out == " foobar"
50
+
51
+ _, out = @grab_output @on_device @mtlprint (:foobar )
52
+ @test out == " foobar"
53
+
54
+ _, out = @grab_output @on_device @mtlprint (" foo" , " bar" )
55
+ @test out == " foobar"
56
+
57
+ _, out = @grab_output @on_device @mtlprint (" foobar " , 42 )
58
+ @test out == " foobar 42"
59
+
60
+ _, out = @grab_output @on_device @mtlprint (" foobar $(42 ) " )
61
+ @test out == " foobar 42"
62
+
63
+ _, out = @grab_output @on_device @mtlprint (" foobar $(4 ) " , 2 )
64
+ @test out == " foobar 42"
65
+
66
+ _, out = @grab_output @on_device @mtlprint (" foobar " , 4 , " $(2 ) " )
67
+ @test out == " foobar 42"
68
+
69
+ _, out = @grab_output @on_device @mtlprint (42 )
70
+ @test out == " 42"
71
+
72
+ _, out = @grab_output @on_device @mtlprint (4 , 2 )
73
+ @test out == " 42"
74
+
75
+ _, out = @grab_output @on_device @mtlprint (Any)
76
+ @test out == " Any"
77
+
78
+ _, out = @grab_output @on_device @mtlprintln (" foobar $(42 ) " )
79
+ @test out == " foobar 42\n "
80
+
81
+
82
+ # argument types
83
+
84
+ # we're testing the generated functions now, so can't use literals
85
+ function test_output (val, str)
86
+ canary = rand (Int32) # if we mess up the main arg, this one will print wrong
87
+ _, out = @grab_output @on_device @mtlprint (val, " (" , canary, " )" )
88
+ @test out == " $(str) ($(Int (canary)) )"
89
+ end
90
+
91
+ for typ in (Int16, Int32, Int64, UInt16, UInt32, UInt64)
92
+ test_output (typ (42 ), " 42" )
93
+ end
94
+
95
+ for typ in (Float32,)
96
+ test_output (typ (42 ), " 42.000000" )
97
+ end
98
+
99
+ test_output (Cchar (' c' ), " c" )
100
+
101
+ for typ in (Ptr{Cvoid}, Ptr{Int})
102
+ ptr = convert (typ, Int (0x12345 ))
103
+ test_output (ptr, " 0x12345" )
104
+ end
105
+
106
+ test_output (true , " 1" )
107
+ test_output (false , " 0" )
108
+
109
+ test_output ((1 ,), " (1,)" )
110
+ test_output ((1 ,2 ), " (1, 2)" )
111
+ test_output ((1 ,2 ,3.0f0 ), " (1, 2, 3.000000)" )
112
+
113
+ # escaping
114
+
115
+ kernel1 (val) = (@mtlprint (val); nothing )
116
+ _, out = @grab_output @on_device kernel1 (42 )
117
+ @test out == " 42"
118
+
119
+ kernel2 (val) = (@mtlprintln (val); nothing )
120
+ _, out = @grab_output @on_device kernel2 (42 )
121
+ @test out == " 42\n "
122
+ end
123
+
124
+ @testset " @mtlshow" begin
125
+ function kernel ()
126
+ seven_i32 = Int32 (7 )
127
+ three_f32 = Float32 (3 )
128
+ @mtlshow seven_i32
129
+ @mtlshow three_f32
130
+ @mtlshow 1f0 + 4f0
131
+ return
132
+ end
133
+
134
+ _, out = @grab_output @on_device kernel ()
135
+ @test out == " seven_i32 = 7\n three_f32 = 3.000000\n 1.0f0 + 4.0f0 = 5.000000\n "
136
+ end
137
+ end
138
+
0 commit comments