File: ispc-library.cpp

package info (click to toggle)
ispc 1.28.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 97,620 kB
  • sloc: cpp: 77,067; python: 8,303; yacc: 3,337; lex: 1,126; ansic: 631; sh: 475; makefile: 17
file content (110 lines) | stat: -rw-r--r-- 3,820 bytes parent folder | download
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
// This test verifies that API for ISPC C++ library works correctly
// and without state corruption.

// RUN: %{cxx} -x c++ -std=c++17 -I%{ispc_include} %s -L%{ispc_lib} -lispc -o %t.bin
// RUN: env LD_LIBRARY_PATH=%{ispc_lib} %t.bin | FileCheck %s

// REQUIRES: LINUX_HOST

// CHECK: ISPC C++ API Test Starting
// CHECK: Basic compilation test: SUCCESS
// CHECK: Multiple compilation test: SUCCESS
// CHECK: Engine isolation test: SUCCESS
// CHECK: All tests completed successfully

// REQUIRES: ISPC_LIBRARY && !ASAN_RUN

#include <iostream>
#include <vector>
#include <string>
#include <memory>
#include <fstream>
#include "ispc/ispc.h"

int main() {
    std::cout << "ISPC C++ API Test Starting\n";

    // Initialize ISPC
    if (!ispc::Initialize()) {
        std::cerr << "Failed to initialize ISPC\n";
        return 1;
    }

    bool all_tests_passed = true;

    // Test 1: Basic compilation - create a simple ISPC program in memory and compile it
    // We'll write a simple ISPC file first
    {
        // Create a simple test ISPC file
        std::ofstream test_file("simple_test.ispc");
        test_file << "export void test_func(uniform float vin[], uniform float vout[], uniform int count) {\n";
        test_file << "    foreach (i = 0 ... count) {\n";
        test_file << "        vout[i] = vin[i] * 2.0f;\n";
        test_file << "    }\n";
        test_file << "}\n";
        test_file.close();

        std::vector<std::string> args = {"simple_test.ispc", "--target=host", "-o", "test1.o"};
        int result = ispc::CompileFromArgs(args);
        if (result == 0) {
            std::cout << "Basic compilation test: SUCCESS\n";
        } else {
            std::cout << "Basic compilation test: FAILED\n";
            all_tests_passed = false;
        }
    }

    // Test 2: Multiple compilations with different options
    {
        std::vector<std::string> args1 = {"simple_test.ispc", "--target=host", "-O2", "-o", "test2a.o"};
        std::vector<std::string> args2 = {"simple_test.ispc", "--target=host", "-O0", "--emit-asm", "-o", "test2b.s"};

        int result1 = ispc::CompileFromArgs(args1);
        int result2 = ispc::CompileFromArgs(args2);

        if (result1 == 0 && result2 == 0) {
            std::cout << "Multiple compilation test: SUCCESS\n";
        } else {
            std::cout << "Multiple compilation test: FAILED\n";
            all_tests_passed = false;
        }
    }

    // Test 3: Engine isolation test
    {
        std::vector<std::string> engine_args1 = {"simple_test.ispc", "--target=host", "-O2", "-o", "engine1.o"};
        std::vector<std::string> engine_args2 = {"simple_test.ispc", "--target=host", "-O0", "-o", "engine2.o"};
        std::vector<std::string> engine_args3 = {"simple_test.ispc", "--target=host", "--emit-asm", "-o", "engine3.s"};

        auto engine1 = ispc::ISPCEngine::CreateFromArgs(engine_args1);
        auto engine2 = ispc::ISPCEngine::CreateFromArgs(engine_args2);
        auto engine3 = ispc::ISPCEngine::CreateFromArgs(engine_args3);

        bool engines_created = engine1 && engine2 && engine3;
        bool engines_executed = false;

        if (engines_created) {
            int r1 = engine1->Execute();
            int r2 = engine2->Execute();
            int r3 = engine3->Execute();
            engines_executed = (r1 == 0 && r2 == 0 && r3 == 0);
        }

        if (engines_created && engines_executed) {
            std::cout << "Engine isolation test: SUCCESS\n";
        } else {
            std::cout << "Engine isolation test: FAILED\n";
            all_tests_passed = false;
        }
    }

    ispc::Shutdown();

    if (all_tests_passed) {
        std::cout << "All tests completed successfully\n";
        return 0;
    } else {
        std::cout << "Some tests failed\n";
        return 1;
    }
}