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

39 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-28 04:01 +0000

1# -------------------------------------------------------------------------------------------------- 

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 

9import pytest 

10 

11from tsfpga.vivado.generics import ( 

12 BitVectorGenericValue, 

13 StringGenericValue, 

14 get_vivado_tcl_generic_value, 

15) 

16 

17 

18def test_boolean_generics(): 

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

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

21 

22 

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" 

27 

28 

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" 

33 

34 

35def test_bit_vector_generics(): 

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

37 

38 with pytest.raises(ValueError) as exception_info: 

39 BitVectorGenericValue(1010) 

40 assert ( 

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

42 ) 

43 

44 with pytest.raises(ValueError) as exception_info: 

45 BitVectorGenericValue("102A") 

46 assert ( 

47 str(exception_info.value) 

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

49 ) 

50 

51 

52def test_string_generics(): 

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

54 

55 with pytest.raises(ValueError) as exception_info: 

56 StringGenericValue(3) 

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

58 

59 with pytest.raises(ValueError) as exception_info: 

60 StringGenericValue("apa hest") 

61 assert ( 

62 str(exception_info.value) 

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

64 ) 

65 

66 

67def test_unsupported_generic_type(): 

68 with pytest.raises(ValueError) as exception_info: 

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

70 assert ( 

71 str(exception_info.value) 

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

73 ) 

74 

75 with pytest.raises(ValueError) as exception_info: 

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

77 assert ( 

78 str(exception_info.value) 

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

80 ) 

81 

82 with pytest.raises(ValueError) as exception_info: 

83 get_vivado_tcl_generic_value("01101") 

84 assert ( 

85 str(exception_info.value) 

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

87 )