codegen.cpp 2.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
16 17 18 19 20 21 22 23

#include <sstream>
#include <string>
#include <vector>

#include "gtest/gtest.h"

#include "ngraph/codegen/compiler.hpp"
Robert Kimball's avatar
Robert Kimball committed
24
#include "ngraph/codegen/execution_engine.hpp"
25 26

using namespace std;
Robert Kimball's avatar
Robert Kimball committed
27
using namespace ngraph;
28

29
TEST(DISABLED_codegen, simple_return)
30 31 32
{
    constexpr auto source = R"(extern "C" int test() { return 2+5; })";

Robert Kimball's avatar
Robert Kimball committed
33 34 35 36
    codegen::Compiler compiler;
    codegen::ExecutionEngine execution_engine;

    auto module = compiler.compile(source);
37 38
    ASSERT_NE(nullptr, module);

Robert Kimball's avatar
Robert Kimball committed
39
    execution_engine.add_module(module);
40

Robert Kimball's avatar
Robert Kimball committed
41
    execution_engine.finalize();
42

Robert Kimball's avatar
Robert Kimball committed
43
    auto func = execution_engine.find_function<int()>("test");
44 45 46 47 48 49
    ASSERT_NE(nullptr, func);

    int result = func();
    EXPECT_EQ(7, result);
}

50
TEST(DISABLED_codegen, pass_args)
51 52 53
{
    constexpr auto source = R"(extern "C" int test(int a, int b) { return a+b; })";

Robert Kimball's avatar
Robert Kimball committed
54 55 56 57
    codegen::Compiler compiler;
    codegen::ExecutionEngine execution_engine;

    auto module = compiler.compile(source);
58 59
    ASSERT_NE(nullptr, module);

Robert Kimball's avatar
Robert Kimball committed
60
    execution_engine.add_module(module);
61

Robert Kimball's avatar
Robert Kimball committed
62
    execution_engine.finalize();
63

Robert Kimball's avatar
Robert Kimball committed
64
    auto func = execution_engine.find_function<int(int, int)>("test");
65 66 67 68 69 70
    ASSERT_NE(nullptr, func);

    int result = func(20, 22);
    EXPECT_EQ(42, result);
}

71
TEST(DISABLED_codegen, include)
72 73 74 75 76 77 78 79 80 81
{
    constexpr auto source =
        R"(
        #include <cmath>
        extern "C" int test(int a, int b)
        {
            return (int)pow((double)a,(double)b);
        }
    )";

Robert Kimball's avatar
Robert Kimball committed
82 83 84 85
    codegen::Compiler compiler;
    codegen::ExecutionEngine execution_engine;

    auto module = compiler.compile(source);
86 87
    ASSERT_NE(nullptr, module);

Robert Kimball's avatar
Robert Kimball committed
88
    execution_engine.add_module(module);
89

Robert Kimball's avatar
Robert Kimball committed
90
    execution_engine.finalize();
91

Robert Kimball's avatar
Robert Kimball committed
92
    auto func = execution_engine.find_function<int(int, int)>("test");
93 94 95 96 97
    ASSERT_NE(nullptr, func);

    int result = func(20, 2);
    EXPECT_EQ(400, result);
}