|
| 1 | +/// Copyright 2020 The SVUT Authors |
| 2 | +/// |
| 3 | +/// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +/// of this software and associated documentation files (the "Software"), to |
| 5 | +/// deal in the Software without restriction, including without limitation the |
| 6 | +/// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | +/// sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +/// furnished to do so, subject to the following conditions: |
| 9 | +/// |
| 10 | +/// The above copyright notice and this permission notice shall be included in |
| 11 | +/// all copies or substantial portions of the Software. |
| 12 | +/// |
| 13 | +/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 | +/// IN THE SOFTWARE. |
| 20 | + |
| 21 | + |
| 22 | +`ifndef SVUT_DEFINES |
| 23 | +`define SVUT_DEFINES |
| 24 | + |
| 25 | +/// Define colors for $display |
| 26 | + |
| 27 | +`define BLUE "\033[0;34m" |
| 28 | +`define GREEN "\033[0;32m" |
| 29 | +`define ORANGE "\033[1;33m" |
| 30 | +`define PINK "\033[1;35m" |
| 31 | +`define RED "\033[1;31m" |
| 32 | +`define NC "\033[0m" |
| 33 | + |
| 34 | + |
| 35 | +/// Follows a set of ready to use function to print status |
| 36 | +/// and information with an appropriate color. |
| 37 | + |
| 38 | +`define INFO(msg) \ |
| 39 | + $display("\033[0;34mINFO: %s (@ %0t)\033[0m", msg, $time) |
| 40 | + |
| 41 | +`define SUCCESS(msg) \ |
| 42 | + $display("\033[0;32mSUCCESS: %s (@ %0t)\033[0m", msg, $time) |
| 43 | + |
| 44 | +`define WARNING(msg) \ |
| 45 | + $display("\033[1;33mWARNING: %s (@ %0t)\033[0m", msg, $time); \ |
| 46 | + svut_warning += 1 |
| 47 | + |
| 48 | +`define CRITICAL(msg) \ |
| 49 | + $display("\033[1;35mCRITICAL: %s (@ %0t)\033[0m", msg, $time); \ |
| 50 | + svut_critical += 1 |
| 51 | + |
| 52 | +`define ERROR(msg)\ |
| 53 | + $display("\033[1;31mERROR: %s (@ %0t)\033[0m", msg, $time); \ |
| 54 | + svut_error += 1 |
| 55 | + |
| 56 | +/// SVUT_SETUP is the code portion initializing all the needed |
| 57 | +/// variables. To call once before or after the module instance |
| 58 | + |
| 59 | +`define SVUT_SETUP \ |
| 60 | + integer svut_status = 0; \ |
| 61 | + integer svut_warning = 0; \ |
| 62 | + integer svut_critical = 0; \ |
| 63 | + integer svut_error = 0; \ |
| 64 | + integer svut_error_total = 0; \ |
| 65 | + integer svut_nb_test = 0; \ |
| 66 | + integer svut_nb_test_success = 0; \ |
| 67 | + string svut_test_name = ""; \ |
| 68 | + string svut_suite_name = ""; \ |
| 69 | + string svut_msg = ""; |
| 70 | + |
| 71 | + |
| 72 | +/// LAST_STATUS is a flag asserted if check the last |
| 73 | +/// check function failed |
| 74 | +`define LAST_STATUS svut_status |
| 75 | + |
| 76 | +/// Follows a set of macros to check an expression |
| 77 | +/// or a signal. All use the same syntax: |
| 78 | +/// - a signal or an expression to evaluate |
| 79 | +/// - an optional message to print if case the |
| 80 | +/// evaluation fails. |
| 81 | + |
| 82 | +/// This function is shared between assertions to format messages |
| 83 | +function string create_msg(input string assertion, message); |
| 84 | + if (message != "") |
| 85 | + create_msg = {message, " (", assertion, ")"}; |
| 86 | + else |
| 87 | + create_msg = assertion; |
| 88 | +endfunction |
| 89 | + |
| 90 | + |
| 91 | +/// This check will fails if expression is not = 0 |
| 92 | +`define FAIL_IF(exp, message="") \ |
| 93 | + svut_status = 0; \ |
| 94 | + svut_msg = create_msg("FAIL_IF", message); \ |
| 95 | + if (exp) begin \ |
| 96 | + `ERROR(svut_msg); \ |
| 97 | + svut_status = 1; \ |
| 98 | + end |
| 99 | + |
| 100 | +/// This check will fails if expression is not > 0 |
| 101 | +`define FAIL_IF_NOT(exp, message="") \ |
| 102 | + svut_status = 0; \ |
| 103 | + svut_msg = create_msg("FAIL_IF_NOT", message); \ |
| 104 | + if (!exp) begin \ |
| 105 | + `ERROR(svut_msg); \ |
| 106 | + svut_status = 1; \ |
| 107 | + end |
| 108 | + |
| 109 | +/// This check will fails if both input are equal |
| 110 | +`define FAIL_IF_EQUAL(a,b, message="") \ |
| 111 | + svut_status = 0; \ |
| 112 | + svut_msg = create_msg("FAIL_IF_EQUAL", message); \ |
| 113 | + if (a === b) begin \ |
| 114 | + `ERROR(svut_msg); \ |
| 115 | + svut_status = 1; \ |
| 116 | + end |
| 117 | + |
| 118 | +/// This check will fails if both input are not equal |
| 119 | +`define FAIL_IF_NOT_EQUAL(a,b, message="") \ |
| 120 | + svut_status = 0; \ |
| 121 | + svut_msg = create_msg("FAIL_IF_NOT_EQUAL", message); \ |
| 122 | + if (a !== b) begin \ |
| 123 | + `ERROR(svut_msg); \ |
| 124 | + svut_status = 1; \ |
| 125 | + end |
| 126 | + |
| 127 | +/// This check will fails if expression is not = 0 |
| 128 | +`define ASSERT(exp, message="") \ |
| 129 | + svut_status = 0; \ |
| 130 | + svut_msg = create_msg("ASSERT", message); \ |
| 131 | + if (!exp) begin \ |
| 132 | + `ERROR(svut_msg); \ |
| 133 | + svut_status = 1; \ |
| 134 | + end |
| 135 | + |
| 136 | + |
| 137 | +/// This header must be placed to start a test suite execution |
| 138 | +`define TEST_SUITE(name="") \ |
| 139 | + task run(msg=""); \ |
| 140 | + begin \ |
| 141 | + svut_suite_name = name; \ |
| 142 | + svut_msg = {"Start testsuite ", name}; \ |
| 143 | + `INFO(svut_msg); |
| 144 | + |
| 145 | +/// This header must be placed to start a test execution |
| 146 | +`define UNIT_TEST(name="") \ |
| 147 | + begin \ |
| 148 | + svut_msg = {"Start test ", name}; \ |
| 149 | + `INFO(svut_msg); \ |
| 150 | + setup(); \ |
| 151 | + svut_test_name = name; \ |
| 152 | + svut_error = 0; \ |
| 153 | + svut_nb_test = svut_nb_test + 1; |
| 154 | + |
| 155 | +/// This header must be placed to close a test |
| 156 | +`define UNIT_TEST_END \ |
| 157 | + teardown(); \ |
| 158 | + if (svut_error == 0) begin \ |
| 159 | + svut_nb_test_success = svut_nb_test_success + 1; \ |
| 160 | + `SUCCESS("Test successful"); \ |
| 161 | + end else begin \ |
| 162 | + `ERROR("Test failed"); \ |
| 163 | + svut_error_total += svut_error; \ |
| 164 | + end \ |
| 165 | + end |
| 166 | + |
| 167 | +/// This header must be placed to close a test suite |
| 168 | +`define TEST_SUITE_END \ |
| 169 | + end \ |
| 170 | + endtask \ |
| 171 | + initial begin\ |
| 172 | + run(); \ |
| 173 | + svut_msg = {"Stop testsuite ", svut_suite_name}; \ |
| 174 | + `INFO(svut_msg); \ |
| 175 | + if (svut_warning > 0) begin \ |
| 176 | + $display("\t\033[1;33m- Warning number: %0d\033[0m", svut_warning); \ |
| 177 | + end \ |
| 178 | + if (svut_critical > 0) begin \ |
| 179 | + $display("\t\033[1;35m- Critical number: %0d\033[0m", svut_critical); \ |
| 180 | + end \ |
| 181 | + if (svut_error_total > 0) begin \ |
| 182 | + $display("\t\033[1;31m- Error number: %0d\033[0m", svut_error_total); \ |
| 183 | + end \ |
| 184 | + if (svut_nb_test_success != svut_nb_test) begin \ |
| 185 | + $display("\t\033[1;31m- STATUS: %0d/%0d test(s) passed\033[0m\n", svut_nb_test_success, svut_nb_test); \ |
| 186 | + end else begin \ |
| 187 | + $display("\t\033[0;32m- STATUS: %0d/%0d test(s) passed\033[0m\n", svut_nb_test_success, svut_nb_test); \ |
| 188 | + end \ |
| 189 | + $finish(); \ |
| 190 | + end |
| 191 | + |
| 192 | +`endif |
0 commit comments