Coverage for tsfpga/vivado/test/test_generics.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 20:52 +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# -------------------------------------------------------------------------------------------------- 

8 

9# Third party libraries 

10import pytest 

11 

12# First party libraries 

13from tsfpga.vivado.generics import ( 

14 BitVectorGenericValue, 

15 StringGenericValue, 

16 get_vivado_tcl_generic_value, 

17) 

18 

19 

20def test_boolean_generics(): 

21 assert get_vivado_tcl_generic_value(True) == "1'b1" 

22 assert get_vivado_tcl_generic_value(False) == "1'b0" 

23 

24 

25def test_integer_generics(): 

26 assert get_vivado_tcl_generic_value(123) == "123" 

27 assert get_vivado_tcl_generic_value(0) == "0" 

28 assert get_vivado_tcl_generic_value(-7) == "-7" 

29 

30 

31def test_float_generics(): 

32 assert get_vivado_tcl_generic_value(3.1415) == "3.1415" 

33 assert get_vivado_tcl_generic_value(0.0) == "0.0" 

34 assert get_vivado_tcl_generic_value(-1.72) == "-1.72" 

35 

36 

37def test_bit_vector_generics(): 

38 assert get_vivado_tcl_generic_value(BitVectorGenericValue("1010")) == "4'b1010" 

39 

40 with pytest.raises(ValueError) as exception_info: 

41 BitVectorGenericValue(1010) 

42 assert str(exception_info.value) == ( 

43 "Expected BitVectorGenericValue value to be of type str." 

44 ' Got type="<class \'int\'>", value="1010".' 

45 ) 

46 

47 with pytest.raises(ValueError) as exception_info: 

48 BitVectorGenericValue("102A") 

49 assert ( 

50 str(exception_info.value) 

51 == 'Expected BitVectorGenericValue value to contain only "1" or "0". Got "102A".' 

52 ) 

53 

54 

55def test_string_generics(): 

56 assert get_vivado_tcl_generic_value(StringGenericValue("apa")) == '"apa"' 

57 

58 with pytest.raises(ValueError) as exception_info: 

59 StringGenericValue(3) 

60 assert str(exception_info.value) == ( 

61 "Expected StringGenericValue value to be of type str." 

62 ' Got type="<class \'int\'>", value="3".' 

63 ) 

64 

65 with pytest.raises(ValueError) as exception_info: 

66 StringGenericValue("apa hest") 

67 assert ( 

68 str(exception_info.value) 

69 == 'Expected StringGenericValue value to not contain spaces. Got "apa hest".' 

70 ) 

71 

72 

73def test_unsupported_generic_type(): 

74 with pytest.raises(ValueError) as exception_info: 

75 get_vivado_tcl_generic_value(dict(name="value")) 

76 assert ( 

77 str(exception_info.value) 

78 == "Unsupported type for generic. Got type=\"<class 'dict'>\", value=\"{'name': 'value'}\"." 

79 ) 

80 

81 with pytest.raises(ValueError) as exception_info: 

82 get_vivado_tcl_generic_value("/home/test.vhd") 

83 assert str(exception_info.value) == ( 

84 'Unsupported type for generic. Got type="<class \'str\'>", value="/home/test.vhd".' 

85 " Please use either of the explicit types StringGenericValue or BitVectorGenericValue." 

86 ) 

87 

88 with pytest.raises(ValueError) as exception_info: 

89 get_vivado_tcl_generic_value("01101") 

90 assert str(exception_info.value) == ( 

91 'Unsupported type for generic. Got type="<class \'str\'>", value="01101".' 

92 " Please use either of the explicit types StringGenericValue or BitVectorGenericValue." 

93 )