from __future__ import print_function
import numpy
from numba import cuda
from numba import unittest_support as unittest


def cuhello():
    i = cuda.grid(1)
    print(i, 1.234)


def printfloat():
    i = cuda.grid(1)
    print(i, 23, 34.3, 321)


def cuprintary(A):
    i = cuda.grid(1)
    print("A[", i, "]", A[i])


class TestPrint(unittest.TestCase):
    def test_cuhello(self):
        """
        Eyeballing required
        """
        jcuhello = cuda.jit('void()', debug=False)(cuhello)
        jcuhello[2, 3]()
        cuda.synchronize()

    def test_printfloat(self):
        """
        Eyeballing required
        """
        jprintfloat = cuda.jit('void()', debug=False)(printfloat)
        jprintfloat()
        cuda.synchronize()

    @unittest.skipIf(True, "Print string not implemented yet")
    def test_print_array(self):
        """
        Eyeballing required
        """
        jcuprintary = cuda.jit('void(float32[:])')(cuprintary)
        A = numpy.arange(10, dtype=numpy.float32)
        jcuprintary[2, 5](A)
        cuda.synchronize()


if __name__ == '__main__':
    unittest.main()

@@ -31,16 +31,56 @@
 unction%0A
+%0Aimport contextlib%0Aimport io%0Aimport os%0A%0A
 import n
@@ -84,16 +84,17 @@
 t numpy%0A
+%0A
 from num
@@ -157,16 +157,380 @@
 ttest%0A%0A%0A
+@contextlib.contextmanager%0Adef redirect_fd(fd):%0A    %22%22%22%0A    Temporarily redirect *fd* to a pipe's write end and return a file object%0A    wrapping the pipe's read end.%0A    %22%22%22%0A    save = os.dup(fd)%0A    r, w = os.pipe()%0A    try:%0A        os.dup2(w, fd)%0A        yield io.open(r, %22r%22)%0A    finally:%0A        os.close(w)%0A        os.dup2(save, fd)%0A        os.close(save)%0A%0A%0A
 def cuhe
@@ -574,13 +574,11 @@
 (i, 
-1.234
+999
 )%0A%0A%0A
@@ -754,16 +754,17 @@
 tCase):%0A
+%0A
     def 
@@ -787,60 +787,8 @@
 f):%0A
-        %22%22%22%0A        Eyeballing required%0A        %22%22%22%0A
     
@@ -843,16 +843,59 @@
 uhello)%0A
+        with redirect_fd(1) as stdout:%0A    
         
@@ -911,32 +911,36 @@
 2, 3%5D()%0A        
+    
 cuda.synchronize
@@ -946,91 +946,258 @@
 e()%0A
-%0A    def test_printfloat(self):%0A        %22%22%22%0A        Eyeballing required%0A        %22%22%22
+        # The output of GPU threads is intermingled, just sanity check it%0A        out = stdout.read()%0A        expected = ''.join('%25d 999%5Cn' %25 i for i in range(6))%0A        self.assertEqual(sorted(out), sorted(expected))%0A%0A    def test_printfloat(self):
 %0A   
@@ -1259,16 +1259,59 @@
 tfloat)%0A
+        with redirect_fd(1) as stdout:%0A    
         
@@ -1324,32 +1324,36 @@
 float()%0A        
+  
+  
 cuda.synchronize
@@ -1354,16 +1354,80 @@
 ronize()
+%0A        self.assertEqual(stdout.read(), %220 23 34.300000 321%5Cn%22)
 %0A%0A    @u

Make cuda print() tests programmatic