Hands-on SVUnit Tutorial - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

203


180
 
1
/*
2
                 ------------------------
3
                 SVUnit DEMO INSTRUCTIONS
4
                 ------------------------
5
6
  Time to learn the basics of unit testing with SVUnit!
7
8
  Here's a simple set of unit tests for an interface called
9
  svunitOnSwitch. The purpose of the design is to offer a few
10
  utility functions as well as to operate an on/off output.
11
12
  The API of the svunitOnSwitch is as follows:
13
    output logic on;
14
    function logic true();
15
    function logic false();
16
    function int return43();
17
    turn_on();
18
    turn_off();
19
20
  The svunitOnSwitch is defined over there --------------->
21
22
  The SVTESTs below are the acceptance unit tests that verify
23
  the functionality of the svunitOnSwitch. If you push run (in
24
  the top left corner) you'll see all the tests fail b/c none
25
  of the svunitOnSwitch functionality is implemented. Your
26
  job is to build a complete svunitOnSwitch, 1 requirement at
27
  a time, by:
28
29
    * examining the requirement defined in the unit test
30
      (HINT: a unit test is marked by the `SVTEST macro)
31
    * implementng the corresponding code in the svunitOnSwitch
32
      (HINT: watch for the "no implementation yet" comment)
33
    * running the test suite to make sure your implementation
34
      satisfies the unit test
35
36
  When you've gone through all the tests and your entire test
37
  suite passes, you're done! You've verified the
38
  svunitOnSwitch and learned the basics of SVUnit!
39
40
  The unit tests start a few lines down so scroll down to get
41
  started.
42
43
  Ready... set... go!
44
45
46
  -----------------------------------------------------------
47
  -----------------------------------------------------------
48
49
  You can do a lot more than test a simple svunitOnSwith with
50
  SVUnit. When you're ready to test your own design and
51
  testbench IP visit:
52
53
                 www.AgileSoC.com/svunit
54
55
  -----------------------------------------------------------
56
  -----------------------------------------------------------
57
*/
58
59
`include "svunit_defines.svh"
60
import svunit_pkg::*;
61
62
module svunitDemo_unit_test;
63
  string name = "svunitDemo_ut";
64
65
  `SVUNIT_TESTS_BEGIN
66
67
  //------------------------------
68
  // test: true_test
69
  //   the true() function should
70
  //   return 1
71
  //------------------------------
72
  `SVTEST(true_returns_1)
73
    `FAIL_UNLESS(uut.true() === 1);
74
  `SVTEST_END
75
 
76
 
77
  //------------------------------
78
  // test: false_test
79
  //   the false() function should
80
  //   return 0
81
  //------------------------------
82
  `SVTEST(false_returns_0)
83
    `FAIL_UNLESS(uut.false() === 0);
84
  `SVTEST_END
85
 
86
 
87
  //-----------------------------------
88
  // test: return43
89
  //   The function return43() returns
90
  //   a value. this test should fail
91
  //   if that doesn't happen.
92
  //-----------------------------------
93
  `SVTEST(return43)
94
    `FAIL_UNLESS(uut.return43() === 43);
95
  `SVTEST_END
96
 
97
 
98
  //---------------------------------
99
  // test: turn_on
100
  //   our uut has an output pin
101
  //   called 'on' that we can
102
  //   assert via turn_on()
103
  //---------------------------------
104
  `SVTEST(turn_on)
105
    uut.turn_on();
106
    `FAIL_UNLESS(uut.on === 1);
107
  `SVTEST_END
108
 
109
 
110
  //---------------------------------
111
  // test: turn_off
112
  //   we can turn 'on' off using
113
  //   turn_off() method
114
  //---------------------------------
115
  `SVTEST(turn_off)
116
    uut.turn_off();
117
    `FAIL_UNLESS(uut.on === 0);
118
  `SVTEST_END
119
120
/*
121
  ----------------------------------------
122
  ----------------------------------------
123
124
    For more SVUnit, Remember to visit:
125
126
         www.AgileSoC.com/svunit
127
128
129
    And try the other SVUnit examples at:
130
131
          www.edaplayground.com
132
133
  ----------------------------------------
134
  ----------------------------------------
135
*/
136
137
  `SVUNIT_TESTS_END
138
139
140
  svunit_testcase svunit_ut;
141
142
143
  //===================================
144
  // This is the UUT that we're 
145
  // running the Unit Tests on
146
  //===================================
147
  svunitOnSwitch uut();
148
149
150
  //===================================
151
  // Build. Runs once
152
  //===================================
153
  function void build();
154
    svunit_ut = new(name);
155
  endfunction
156
157
158
  //===================================
159
  // Setup for running the Unit Tests
160
  // Runs before every SVTEST.
161
  //===================================
162
  task setup();
163
    svunit_ut.setup();
164
    /* Place Setup Code Here */
165
  endtask
166
167
168
  //===================================
169
  // Here we deconstruct anything we 
170
  // need after running the Unit Tests
171
  // Runs after every SVTEST.
172
  //===================================
173
  task teardown();
174
    svunit_ut.teardown();
175
    /* Place Teardown Code Here */
176
  endtask
177
178
179
endmodule
180
33
 
1
/*
2
  svunitOnSwitch
3
4
  The API is defined but that's pretty much it! Use
5
  the unit tests to help fill the proper implementation.
6
*/
7
8
interface svunitOnSwitch (
9
  output logic on
10
);
11
  initial on = 'hx;
12
13
  function logic true();
14
    // no implementation yet
15
  endfunction
16
17
  function logic false();
18
    // no implementation yet
19
  endfunction
20
21
  function int return43();
22
    // no implementation yet
23
  endfunction
24
25
  function void turn_on();
26
    // no implementation yet
27
  endfunction
28
29
  function void turn_off();
30
    // no implementation yet
31
  endfunction
32
endinterface
33
4565 views and 0 likes     
 
Follow the instructions to get all the SVUnit tests passing.
Good luck :)

Follow the instructions to get all the SVUnit tests passing.
Good luck :)

2120:0