GCC Code Coverage Report
Directory: generated/vunit_out/preprocessed/ Exec Total Coverage
File: generated/vunit_out/preprocessed/math/tb_math_pkg.vhd Lines: 104 104 100.0 %
Date: 2021-06-12 04:12:08 Branches: 298 426 70.0 %

Line Branch Exec Source
1
78
-- -------------------------------------------------------------------------------------------------
2
-- Copyright (c) Lukas Vik. All rights reserved.
3
--
4
-- This file is part of the tsfpga project.
5
-- https://tsfpga.com
6
-- https://gitlab.com/tsfpga/tsfpga
7
-- -------------------------------------------------------------------------------------------------
8
9
library ieee;
10
use ieee.std_logic_1164.all;
11
use ieee.numeric_std.all;
12
13
library vunit_lib;
14
context vunit_lib.vunit_context;
15
16
use work.math_pkg.all;
17
18
19














866
entity tb_math_pkg is
20
  generic (
21
    runner_cfg : string
22
    );
23
end entity;
24
25
26
architecture tb of tb_math_pkg is
26
27
  -- This function calculates the number of bits pars that differs
28
  -- in the two input vectors.
29
  function hamming_distance(in1, in2 : std_logic_vector) return integer is
30
6858
    variable tmp : std_logic_vector(in1'range);
31
762
    variable ret : integer := 0;
32
  begin
33
762
    tmp := in1 xor in2;
34
762
    for i in tmp'range loop
35
6096
      if tmp(i) = '1' then
36
6096
        ret := ret + 1;
37
      end if;
38
    end loop;
39




































3521
    return ret;
40
  end function;
41
begin
42
43
13
  main : process
44
78
    variable value : signed(5 - 1 downto 0);
45
117
    variable value_slv : unsigned(8 - 1 downto 0);
46
    constant some_integer_vector : integer_vector(0 to 3) := (-1, 4, 0, -7);
47
117
    variable abs_vector_output : integer_vector(0 to 3);
48
  begin
49
39
    test_runner_setup(runner, runner_cfg);
50
51
13
    if run("ceil_log2") then
52
2
      check_equal(ceil_log2(1), 0, line_num => 52, file_name => "tb_math_pkg.vhd");
53
54
2
      check_equal(ceil_log2(2), 1, line_num => 54, file_name => "tb_math_pkg.vhd");
55
56
2
      check_equal(ceil_log2(3), 2, line_num => 56, file_name => "tb_math_pkg.vhd");
57
2
      check_equal(ceil_log2(4), 2, line_num => 57, file_name => "tb_math_pkg.vhd");
58
59
2
      check_equal(ceil_log2(5), 3, line_num => 59, file_name => "tb_math_pkg.vhd");
60
2
      check_equal(ceil_log2(7), 3, line_num => 60, file_name => "tb_math_pkg.vhd");
61
2
      check_equal(ceil_log2(8), 3, line_num => 61, file_name => "tb_math_pkg.vhd");
62
63
3
      check_equal(ceil_log2(9), 4, line_num => 63, file_name => "tb_math_pkg.vhd");
64
65
12
    elsif run("log2") then
66
2
      check_equal(log2(1), 0, line_num => 66, file_name => "tb_math_pkg.vhd");
67
2
      check_equal(log2(2), 1, line_num => 67, file_name => "tb_math_pkg.vhd");
68
2
      check_equal(log2(32), 5, line_num => 68, file_name => "tb_math_pkg.vhd");
69
2
      check_equal(log2(64), 6, line_num => 69, file_name => "tb_math_pkg.vhd");
70
3
      check_equal(log2(128), 7, line_num => 70, file_name => "tb_math_pkg.vhd");
71
72
11
    elsif run("num_bits_needed_int") then
73
2
      check_equal(num_bits_needed(0), 1, line_num => 73, file_name => "tb_math_pkg.vhd");
74
2
      check_equal(num_bits_needed(1), 1, line_num => 74, file_name => "tb_math_pkg.vhd");
75
2
      check_equal(num_bits_needed(2), 2, line_num => 75, file_name => "tb_math_pkg.vhd");
76
2
      check_equal(num_bits_needed(3), 2, line_num => 76, file_name => "tb_math_pkg.vhd");
77
78
2
      check_equal(num_bits_needed(6), 3, line_num => 78, file_name => "tb_math_pkg.vhd");
79
2
      check_equal(num_bits_needed(7), 3, line_num => 79, file_name => "tb_math_pkg.vhd");
80
2
      check_equal(num_bits_needed(8), 4, line_num => 80, file_name => "tb_math_pkg.vhd");
81
3
      check_equal(num_bits_needed(9), 4, line_num => 81, file_name => "tb_math_pkg.vhd");
82
83
10
    elsif run("round_up_to_power_of_two") then
84
2
      check_equal(round_up_to_power_of_two(1), 1, line_num => 84, file_name => "tb_math_pkg.vhd");
85
86
2
      check_equal(round_up_to_power_of_two(2), 2, line_num => 86, file_name => "tb_math_pkg.vhd");
87
88
2
      check_equal(round_up_to_power_of_two(3), 4, line_num => 88, file_name => "tb_math_pkg.vhd");
89
2
      check_equal(round_up_to_power_of_two(4), 4, line_num => 89, file_name => "tb_math_pkg.vhd");
90
91
2
      check_equal(round_up_to_power_of_two(5), 8, line_num => 91, file_name => "tb_math_pkg.vhd");
92
93
2
      check_equal(round_up_to_power_of_two(127), 128, line_num => 93, file_name => "tb_math_pkg.vhd");
94
2
      check_equal(round_up_to_power_of_two(128), 128, line_num => 94, file_name => "tb_math_pkg.vhd");
95
3
      check_equal(round_up_to_power_of_two(129), 256, line_num => 95, file_name => "tb_math_pkg.vhd");
96
97
9
    elsif run("num_bits_needed_vector") then
98
1
      value_slv := "00000000";
99
2
      check_equal(num_bits_needed(value_slv), 1, line_num => 99, file_name => "tb_math_pkg.vhd");
100
101
1
      value_slv := "00000001";
102
2
      check_equal(num_bits_needed(value_slv), 1, line_num => 102, file_name => "tb_math_pkg.vhd");
103
104
1
      value_slv := "00000010";
105
2
      check_equal(num_bits_needed(value_slv), 2, line_num => 105, file_name => "tb_math_pkg.vhd");
106
107
1
      value_slv := "00000011";
108
2
      check_equal(num_bits_needed(value_slv), 2, line_num => 108, file_name => "tb_math_pkg.vhd");
109
110
1
      value_slv := "00000100";
111
3
      check_equal(num_bits_needed(value_slv), 3, line_num => 111, file_name => "tb_math_pkg.vhd");
112
113
8
    elsif run("lt_0") then
114
1
      value := to_signed(-3, value'length);
115

2
      check_true(lt_0(value), line_num => 115, file_name => "tb_math_pkg.vhd");
116
1
      value := to_signed(0, value'length);
117

2
      check_false(lt_0(value), line_num => 117, file_name => "tb_math_pkg.vhd");
118
1
      value := to_signed(3, value'length);
119

3
      check_false(lt_0(value), line_num => 119, file_name => "tb_math_pkg.vhd");
120
121
7
    elsif run("geq_0") then
122
1
      value := to_signed(-3, value'length);
123

2
      check_false(geq_0(value), line_num => 123, file_name => "tb_math_pkg.vhd");
124
1
      value := to_signed(0, value'length);
125

2
      check_true(geq_0(value), line_num => 125, file_name => "tb_math_pkg.vhd");
126
1
      value := to_signed(3, value'length);
127

3
      check_true(geq_0(value), line_num => 127, file_name => "tb_math_pkg.vhd");
128
129
6
    elsif run("to_and_from_gray") then
130
1
      for i in 1 to 2 ** value_slv'length - 2 loop
131

254
        value_slv := to_unsigned(i, value_slv'length);
132
508
        check_equal(from_gray(to_gray(value_slv)), value_slv, line_num => 132, file_name => "tb_math_pkg.vhd");
133
        -- Verify that only one bit changes when incrementing the input
134
        -- to to_gray
135
508
        check_equal(hamming_distance(to_gray(value_slv), to_gray(value_slv + 1)), 1, line_num => 135, file_name => "tb_math_pkg.vhd");
136
508
        check_equal(hamming_distance(to_gray(value_slv - 1), to_gray(value_slv)), 1, line_num => 136, file_name => "tb_math_pkg.vhd");
137

1017
        check_equal(hamming_distance(to_gray(value_slv - 1), to_gray(value_slv + 1)), 2, line_num => 137, file_name => "tb_math_pkg.vhd");
138
      end loop;
139
140
5
    elsif run("is_power_of_two") then
141

2
      check_true(is_power_of_two(2), line_num => 141, file_name => "tb_math_pkg.vhd");
142

2
      check_true(is_power_of_two(4), line_num => 142, file_name => "tb_math_pkg.vhd");
143

2
      check_true(is_power_of_two(16), line_num => 143, file_name => "tb_math_pkg.vhd");
144
145

2
      check_false(is_power_of_two(15), line_num => 145, file_name => "tb_math_pkg.vhd");
146

3
      check_false(is_power_of_two(17), line_num => 146, file_name => "tb_math_pkg.vhd");
147
148
4
    elsif run("abs_vector") then
149
1
      abs_vector_output := abs_vector(some_integer_vector);
150
1
      for idx in some_integer_vector'range loop
151


17
        check_equal(abs_vector_output(idx), abs(some_integer_vector(idx)), line_num => 151, file_name => "tb_math_pkg.vhd");
152
      end loop;
153
154
3
    elsif run("vector_sum") then
155
2
      check_equal(vector_sum((0, 1, -4)), -3, line_num => 155, file_name => "tb_math_pkg.vhd");
156
3
      check_equal(vector_sum((4, 1, 3)), 8, line_num => 156, file_name => "tb_math_pkg.vhd");
157
158
2
    elsif run("greatest_common_divisor") then
159
2
      check_equal(greatest_common_divisor(6, 3), 3, line_num => 159, file_name => "tb_math_pkg.vhd");
160
2
      check_equal(greatest_common_divisor(7, 3), 1, line_num => 160, file_name => "tb_math_pkg.vhd");
161
2
      check_equal(greatest_common_divisor(7, 1), 1, line_num => 161, file_name => "tb_math_pkg.vhd");
162
3
      check_equal(greatest_common_divisor(8, 15), 1, line_num => 162, file_name => "tb_math_pkg.vhd");
163
164
14
    elsif run("is_mutual_prime") then
165
2
      check_equal(is_mutual_prime(6, (3, 7)), false, line_num => 165, file_name => "tb_math_pkg.vhd");
166
2
      check_equal(is_mutual_prime(7, (3, 6)), true, line_num => 166, file_name => "tb_math_pkg.vhd");
167
2
      check_equal(is_mutual_prime(7, (1, 5)), true, line_num => 167, file_name => "tb_math_pkg.vhd");
168
3
      check_equal(is_mutual_prime(8, (3, 7)), true, line_num => 168, file_name => "tb_math_pkg.vhd");
169
    end if;
170
171
2759
    test_runner_cleanup(runner);
172
  end process;
173
174
end architecture;