summaryrefslogtreecommitdiff
path: root/xnt/tests/compilercollectiontests.py
blob: cb5b3fcf825bf3c90b3d796c85fba3b17df7b176 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
"""Test Common Compiler Collection"""

#   Xnt -- A Wrapper Build Tool
#   Copyright (C) 2013  Kenny Ballou

#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.

#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import shutil
import xnt
import xnt.build.cc as cc
import unittest

#pylint: disable-msg=C0103
@unittest.skipUnless(xnt.in_path("gcc"), "gcc is not in your path")
class GccTests(unittest.TestCase):
    """Test GCC"""
    def setUp(self):
        """Test Case Setup"""
        os.mkdir("temp")
        with open("temp/hello.c", "w") as test_code:
            test_code.write("""
            #include <stdio.h>
            int main() {
                printf("Hello, World!\\n");
                return 0;
            }
            """)

    def tearDown(self):
        """Test Case Teardown"""
        shutil.rmtree("temp")

    def test_gcc(self):
        """Test Default GCC"""
        cwd = os.getcwd()
        os.chdir("temp")
        cc.gcc("hello.c")
        self.assertTrue(os.path.isfile("a.out"))
        os.chdir(cwd)

    def test_gcc_o(self):
        """Test GCC with output"""
        cc.gcc_o("temp/hello.c", "temp/hello")
        self.assertTrue(os.path.isfile("temp/hello"))

@unittest.skipUnless(xnt.in_path("g++"), "g++ is not in your path")
class GppTests(unittest.TestCase):
    """Test G++ (C++ GCC)"""
    def setUp(self):
        """Test Case Setup"""
        os.mkdir("temp")
        with open("temp/hello.cpp", "w") as test_code:
            test_code.write("""
            #include <iostream>
            int main() {
                std::cout << "Hello, World!" << std::endl;
                return 0;
            }
            """)

    def tearDown(self):
        """Test Case Teardown"""
        shutil.rmtree("temp")

    def test_gpp(self):
        """Test Default G++"""
        cwd = os.getcwd()
        os.chdir("temp")
        cc.gpp("hello.cpp")
        self.assertTrue("a.out")
        os.chdir(cwd)

    def test_gpp_o(self):
        """Test G++ with output"""
        cc.gpp_o("temp/hello.cpp", "temp/hello")
        self.assertTrue(os.path.isfile("temp/hello"))

@unittest.skipUnless(xnt.in_path('nvcc'), 'nvcc is not in your path')
class NvccTests(unittest.TestCase):
    """Test NVCC"""
    def setUp(self):
        """Test Case Setup"""
        os.mkdir("temp")
        with open("temp/hello.cu", "w") as test_code:
            test_code.write("""
            __global__ void kernel(float *x) {
                int idx = threadIdx.x;
                x[idx] = 42;
            }
            int main() {
                int size = sizeof(float) * 128;
                float *x = (float*)malloc(size);
                float *dev_x;
                cudaMalloc((void**)&dev_x, size);
                cudaMemcpy(dev_x, x, size, cudaMemcpyHostToDevice);
                kernel<<<128, 1>>>(dev_x);
                cudaMemcpy(x, dev_x, size, cudaMemcpyDeviceToHost);
                cudaFree(dev_x);
            }""")
    def tearDown(self):
        """Test Case Teardown"""
        shutil.rmtree("temp")

    def test_nvcc(self):
        """Test Default NVCC"""
        cwd = os.getcwd()
        os.chdir("temp")
        cc.nvcc("hello.cu")
        self.assertTrue(os.path.isfile("a.out"))
        os.chdir(cwd)

    def test_nvcc_o(self):
        """Test Named Output NVCC"""
        cc.nvcc_o("temp/hello.cu", "temp/hello")
        self.assertTrue(os.path.isfile("temp/hello"))

@unittest.skipUnless(xnt.in_path("javac"), "javac is not in your path")
class JavacTests(unittest.TestCase):
    """Test Javac"""
    def setUp(self):
        """Test Case Setup"""
        os.mkdir("temp")
        with open("temp/hello.java", "w") as test_code:
            test_code.write("""
            class hello {
                public static void main(String[] args) {
                    System.out.println("Hello, World!");
                }
            }
            """)

    def tearDown(self):
        """Test Case Teardown"""
        shutil.rmtree("temp")

    def test_javac(self):
        """Test Default Javac"""
        cwd = os.getcwd()
        os.chdir("temp")
        cc.javac("hello.java")
        self.assertTrue(os.path.isfile("hello.class"))
        os.chdir(cwd)

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