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

39 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-29 20:01 +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://gitlab.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 ( 

43 str(exception_info.value) == "Expected BitVectorGenericValue value to be of type str: 1010" 

44 ) 

45 

46 with pytest.raises(ValueError) as exception_info: 

47 BitVectorGenericValue("102A") 

48 assert ( 

49 str(exception_info.value) 

50 == 'Expected BitVectorGenericValue value to contain only "1" or "0": 102A' 

51 ) 

52 

53 

54def test_string_generics(): 

55 assert get_vivado_tcl_generic_value(StringGenericValue("apa")) == "apa" 

56 

57 with pytest.raises(ValueError) as exception_info: 

58 StringGenericValue(3) 

59 assert str(exception_info.value) == "Expected StringGenericValue value to be of type str: 3" 

60 

61 with pytest.raises(ValueError) as exception_info: 

62 StringGenericValue("apa hest") 

63 assert ( 

64 str(exception_info.value) 

65 == "Expected StringGenericValue value to not contain spaces: apa hest" 

66 ) 

67 

68 

69def test_unsupported_generic_type(): 

70 with pytest.raises(ValueError) as exception_info: 

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

72 assert ( 

73 str(exception_info.value) 

74 == "Got unsupported type for generic. Type=<class 'dict'>, value={'name': 'value'}." 

75 ) 

76 

77 with pytest.raises(ValueError) as exception_info: 

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

79 assert ( 

80 str(exception_info.value) 

81 == "Got unsupported type for generic. Type=<class 'str'>, value=/home/test.vhd." 

82 ) 

83 

84 with pytest.raises(ValueError) as exception_info: 

85 get_vivado_tcl_generic_value("01101") 

86 assert ( 

87 str(exception_info.value) 

88 == "Got unsupported type for generic. Type=<class 'str'>, value=01101." 

89 )