1 // Copyright 2017-2019 VMware, Inc.
2 // SPDX-License-Identifier: BSD-2-Clause
3 //
4 // The BSD-2 license (the License) set forth below applies to all parts of the
5 // Cascade project.  You may not use this file except in compliance with the
6 // License.
7 //
8 // BSD-2 License
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this
14 // list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright notice,
17 // this list of conditions and the following disclaimer in the documentation
18 // and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND
21 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include "harness.h"
32 
33 #include "cl/cl.h"
34 #include "common/system.h"
35 #include "gtest/gtest.h"
36 #include "include/cascade.h"
37 
38 using namespace cascade;
39 using namespace cascade::cl;
40 using namespace std;
41 
42 namespace {
43 
44 auto& march = StrArg<string>::create("--march")
45   .initial("minimal");
46 auto& quartus_host = StrArg<string>::create("--quartus_host")
47   .initial("localhost");
48 auto& quartus_port = StrArg<uint32_t>::create("--quartus_port")
49   .initial(9900);
50 
51 } // namespace
52 
53 namespace cascade {
54 
run_parse(const string & path,bool expected)55 void run_parse(const string& path, bool expected) {
56   run_typecheck("regression/minimal", path, expected);
57 }
58 
run_typecheck(const string & march,const string & path,bool expected)59 void run_typecheck(const string& march, const string& path, bool expected) {
60   Cascade c;
61   c.set_fopen_dirs(System::src_root());
62   if (!expected) {
63     c.set_stderr(cout.rdbuf());
64   }
65   c.run();
66 
67   c << "`include \"share/cascade/march/" << march << ".v\"\n"
68     << "`include \"" << path << "\"" << endl;
69 
70   c.stop_now();
71   EXPECT_EQ(c.bad(), expected);
72 }
73 
run_code(const string & march,const string & path,const string & expected)74 void run_code(const string& march, const string& path, const string& expected) {
75   auto* sb = new stringbuf();
76 
77   Cascade c;
78   c.set_fopen_dirs(System::src_root());
79   c.set_stdout(sb);
80   c.set_stderr(cout.rdbuf());
81   c.run();
82 
83   c << "`include \"share/cascade/march/" << march << ".v\"\n"
84     << "`include \"" << path << "\"" << endl;
85 
86   c.stop_now();
87   ASSERT_FALSE(c.bad());
88 
89   c.run();
90   c.wait_for_stop();
91   EXPECT_EQ(sb->str(), expected);
92 }
93 
run_concurrent(const string & march,const string & path,const string & expected)94 void run_concurrent(const string& march, const string& path, const string& expected) {
95   std::thread t1(run_code, march, path, expected);
96   std::thread t2(run_code, march, path, expected);
97   t1.join();
98   t2.join();
99 }
100 
run_benchmark(const string & path,const string & expected)101 void run_benchmark(const string& path, const string& expected) {
102   auto* sb = new stringbuf();
103 
104   Cascade c;
105   c.set_fopen_dirs(System::src_root());
106   c.set_stdout(sb);
107   c.set_stderr(cout.rdbuf());
108   c.set_quartus_server(::quartus_host.value(), ::quartus_port.value());
109   c.run();
110 
111   c << "`include \"share/cascade/march/" << ::march.value() << ".v\"\n"
112     << "`include \"" << path << "\"" << endl;
113 
114   c.stop_now();
115   ASSERT_FALSE(c.bad());
116 
117   c.run();
118   c.wait_for_stop();
119   EXPECT_EQ(sb->str(), expected);
120 }
121 
122 } // namespace cascade
123