Coverage for tsfpga/vivado/test/test_generics.py: 100%
39 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-21 20:51 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-21 20:51 +0000
1# --------------------------------------------------------------------------------------------------
2# Copyright (c) Lukas Vik. All rights reserved.
3#
4# This file is part of the tsfpga project, a project platform for modern FPGA development.
5# https://tsfpga.com
6# https://github.com/tsfpga/tsfpga
7# --------------------------------------------------------------------------------------------------
9import pytest
11from tsfpga.vivado.generics import (
12 BitVectorGenericValue,
13 StringGenericValue,
14 get_vivado_tcl_generic_value,
15)
18def test_boolean_generics():
19 assert get_vivado_tcl_generic_value(True) == "1'b1"
20 assert get_vivado_tcl_generic_value(False) == "1'b0"
23def test_integer_generics():
24 assert get_vivado_tcl_generic_value(123) == "123"
25 assert get_vivado_tcl_generic_value(0) == "0"
26 assert get_vivado_tcl_generic_value(-7) == "-7"
29def test_float_generics():
30 assert get_vivado_tcl_generic_value(3.1415) == "3.1415"
31 assert get_vivado_tcl_generic_value(0.0) == "0.0"
32 assert get_vivado_tcl_generic_value(-1.72) == "-1.72"
35def test_bit_vector_generics():
36 assert get_vivado_tcl_generic_value(BitVectorGenericValue("1010")) == "4'b1010"
38 with pytest.raises(TypeError) as exception_info:
39 BitVectorGenericValue(1010)
40 assert str(exception_info.value) == (
41 "Expected BitVectorGenericValue value to be of type str."
42 ' Got type="<class \'int\'>", value="1010".'
43 )
45 with pytest.raises(ValueError) as exception_info:
46 BitVectorGenericValue("102A")
47 assert (
48 str(exception_info.value)
49 == 'Expected BitVectorGenericValue value to contain only "1" or "0". Got "102A".'
50 )
53def test_string_generics():
54 assert get_vivado_tcl_generic_value(StringGenericValue("apa")) == '"apa"'
56 with pytest.raises(TypeError) as exception_info:
57 StringGenericValue(3)
58 assert str(exception_info.value) == (
59 "Expected StringGenericValue value to be of type str."
60 ' Got type="<class \'int\'>", value="3".'
61 )
63 with pytest.raises(ValueError) as exception_info:
64 StringGenericValue("apa hest")
65 assert (
66 str(exception_info.value)
67 == 'Expected StringGenericValue value to not contain spaces. Got "apa hest".'
68 )
71def test_unsupported_generic_type():
72 with pytest.raises(TypeError) as exception_info:
73 get_vivado_tcl_generic_value({"name": "value"})
74 assert (
75 str(exception_info.value)
76 == "Unsupported type for generic. Got type=\"<class 'dict'>\", value=\"{'name': 'value'}\"."
77 )
79 with pytest.raises(TypeError) as exception_info:
80 get_vivado_tcl_generic_value("/home/test.vhd")
81 assert str(exception_info.value) == (
82 'Unsupported type for generic. Got type="<class \'str\'>", value="/home/test.vhd".'
83 " Please use either of the explicit types StringGenericValue or BitVectorGenericValue."
84 )
86 with pytest.raises(TypeError) as exception_info:
87 get_vivado_tcl_generic_value("01101")
88 assert str(exception_info.value) == (
89 'Unsupported type for generic. Got type="<class \'str\'>", value="01101".'
90 " Please use either of the explicit types StringGenericValue or BitVectorGenericValue."
91 )