1 // -*- mode: C++ -*-
2 //
3 // Copyright (c) 2008, 2009, 2010, 2011, 2015, 2017 The University of Utah
4 // All rights reserved.
5 //
6 // This file is part of `csmith', a random generator of C programs.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 //   * Redistributions of source code must retain the above copyright notice,
12 //     this list of conditions and the following disclaimer.
13 //
14 //   * Redistributions in binary form must reproduce the above copyright
15 //     notice, this list of conditions and the following disclaimer in the
16 //     documentation and/or other materials provided with the distribution.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #if HAVE_CONFIG_H
31 #  include <config.h>
32 #endif
33 
34 #include "CoverageTestExtension.h"
35 #include <cassert>
36 #include "Type.h"
37 #include "ExtensionValue.h"
38 #include "Constant.h"
39 
40 using namespace std;
41 
42 std::string CoverageTestExtension::array_base_name_ = "a";
43 std::string CoverageTestExtension::array_index_ = "test_index";
44 
CoverageTestExtension(int inputs_size)45 CoverageTestExtension::CoverageTestExtension(int inputs_size)
46 	: inputs_size_(inputs_size)
47 {
48 	assert(inputs_size_ > 0);
49 }
50 
~CoverageTestExtension()51 CoverageTestExtension::~CoverageTestExtension()
52 {
53 	values_.clear();
54 }
55 
56 void
GenerateValues()57 CoverageTestExtension::GenerateValues()
58 {
59 	vector<ExtensionValue *>::iterator i;
60 	for (i = values_.begin(); i != values_.end(); ++i) {
61 		const Type *type = (*i)->get_type();
62 		for (int j = 0; j < inputs_size_; ++j) {
63 			Constant *c = Constant::make_random(type);
64 			test_values_.push_back(c);
65 		}
66 	}
67 }
68 
69 void
output_array_init(std::ostream & out,int count)70 CoverageTestExtension::output_array_init(std::ostream &out, int count)
71 {
72 	if (inputs_size_ == 1) {
73 		test_values_[count]->Output(out);
74 		return;
75 	}
76 	int len = 0;
77 	int last_index = inputs_size_ + count - 1;
78 	for (int i = count; i < last_index; ++i) {
79 		if ((len % 10) == 0) {
80 			out << std::endl;
81 			out << AbsExtension::tab_ << AbsExtension::tab_;
82 		}
83 		test_values_[i]->Output(out);
84 		out << ", ";
85 		len++;
86 	}
87 	if ((len % 10) == 0) {
88 		out << std::endl;
89 		out << AbsExtension::tab_ << AbsExtension::tab_;
90 	}
91 	test_values_[last_index]->Output(out);
92 }
93 
94 void
output_decls(std::ostream & out)95 CoverageTestExtension::output_decls(std::ostream &out)
96 {
97 	AbsExtension::default_output_definitions(out, values_, false);
98 	vector<ExtensionValue *>::iterator i;
99 	int count = 0;
100 	for (i = values_.begin(); i != values_.end(); ++i) {
101 		out << AbsExtension::tab_;
102 		(*i)->get_type()->Output(out);
103 		out << " " << CoverageTestExtension::array_base_name_ << count;
104 		out << "[" << inputs_size_ << "] = {";
105 		output_array_init(out, count);
106 		out << "};" << std::endl;
107 		count++;
108 	}
109 	out << AbsExtension::tab_ << "int " << array_index_ << ";" << std::endl;
110 }
111 
112 void
OutputFirstFunInvocation(std::ostream & out,FunctionInvocation * invoke)113 CoverageTestExtension::OutputFirstFunInvocation(std::ostream &out, FunctionInvocation *invoke)
114 {
115 	out << AbsExtension::tab_ << "for(" << array_index_ << " = 0; ";
116 	out << array_index_ << " < " << inputs_size_ << "; " << array_index_ << "++) {" << std::endl;
117 	vector<ExtensionValue *>::iterator i;
118 	int count = 0;
119 	for (i = values_.begin(); i != values_.end(); ++i) {
120 		out << AbsExtension::tab_ << AbsExtension::tab_;
121 		out << (*i)->get_name() << " = ";
122 		out << CoverageTestExtension::array_base_name_ << count;
123 		out << "[" << array_index_ << "];" << std::endl;
124 		count++;
125 	}
126 	assert(invoke);
127 	out << AbsExtension::tab_ << AbsExtension::tab_;
128 	invoke->Output(out);
129 	out << ";" << std::endl;
130 	out << AbsExtension::tab_ << "}" << std::endl;
131 }
132 
133 void
OutputInit(std::ostream & out)134 CoverageTestExtension::OutputInit(std::ostream &out)
135 {
136 	out << "int main(void)" << endl;
137 	out << "{" << endl;
138 	output_decls(out);
139 	//output_array_init(out);
140 }
141 
142 void
OutputHeader(std::ostream &)143 CoverageTestExtension::OutputHeader(std::ostream &)
144 {
145 	// Nothing to do
146 }
147 
148 void
OutputTail(std::ostream & out)149 CoverageTestExtension::OutputTail(std::ostream &out)
150 {
151 	out << AbsExtension::tab_ << "return 0;" << endl;
152 }
153 
154