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 = MtlArray(ones(2, 2))
30+ # _, out = @grab_output begin
31+ # Metal.@sync @metal kernel(x)
32+ # end
33+ # @test out == "1.000000 1.000000\n" @broken=true
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.), "(1, 2, 3.000000)")
112+
113+
114+ # escaping
115+
116+ kernel1 (val) = (@mtlprint (val); nothing )
117+ _, out = @grab_output @on_device kernel1 (42 )
118+ @test out == " 42"
119+
120+ kernel2 (val) = (@mtlprintln (val); nothing )
121+ _, out = @grab_output @on_device kernel2 (42 )
122+ @test out == " 42\n "
123+ end
124+
125+ # @testset "@mtlshow" begin
126+ # function kernel()
127+ # seven_i32 = Int32(7)
128+ # three_f32 = Float32(3)
129+ # @mtlshow seven_i32
130+ # @mtlshow three_f32
131+ # @mtlshow 1f0 + 4f0
132+ # return
133+ # end
134+
135+ # _, out = @grab_output @on_device kernel()
136+ # @test out == "seven_i32 = 7\nthree_f64 = 3.000000\n1.0f0 + 4.0f0 = 5.000000\n"
137+ # end
138+
139+ # @testset "@mtlshow array pointers" begin
140+ # function kernel()
141+ # a = mtlStaticSharedArray(Float32, 1)
142+ # b = mtlStaticSharedArray(Float32, 2)
143+ # @mtlshow pointer(a) pointer(b)
144+ # return
145+ # end
146+
147+ # _, out = @grab_output @on_device kernel()
148+ # @test ocmtlrsin("pointer(a) = ", out)
149+ # @test ocmtlrsin("pointer(b) = ", out)
150+ # @test ocmtlrsin("= 0", out)
151+ # end
152+
153+ end
154+
0 commit comments