_basic.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # generate assert test for int
  2. assert 0xffff == 65535
  3. assert 0xAAFFFF == 11206655
  4. assert 0x7fffffff == 2147483647
  5. # test == != >= <= < >
  6. # generate 2 cases for each operator
  7. assert -1 == -1
  8. assert -1 != 1
  9. assert -1 >= -1
  10. assert -1 <= -1
  11. assert -1 < 1
  12. assert -1 > -2
  13. # test + - * % ** //
  14. assert -1 + 1 == 0
  15. assert -1 - 1 == -2
  16. assert 4 * -1 == -4
  17. assert 5 % 2 == 1
  18. assert 2 ** 3 == 8
  19. assert 4 // 2 == 2
  20. assert 5 // 2 == 2
  21. # test += -= *= //=
  22. x = 3
  23. x += 1
  24. assert x == 4
  25. x -= 1
  26. assert x == 3
  27. x *= 2
  28. assert x == 6
  29. x //= 2
  30. assert x == 3
  31. # generate assert test for float
  32. def eq(a, b):
  33. dt = a - b
  34. return dt > -0.001 and dt < 0.001
  35. # test + - * / **
  36. assert eq(1.5 + 3, 4.5)
  37. assert eq(1.5 + 3.9, 5.4)
  38. assert eq(5.3 - 2.5, 2.8)
  39. assert eq(0.2**2, 0.04)
  40. assert eq(4**(-1.0), 0.25)
  41. assert eq(2/1, 2)
  42. assert eq(3/2.0, 1.5)
  43. assert eq(1/9, 0.11111)
  44. # test += -= *= /=
  45. x = 3.0
  46. x += 1
  47. assert eq(x, 4.0)
  48. x -= 1
  49. assert eq(x, 3.0)
  50. x *= 2
  51. assert eq(x, 6.0)
  52. x /= 1.8
  53. assert eq(x, 3.3333)
  54. # generate assert test for bool
  55. assert True == True
  56. assert True != False
  57. assert False == False
  58. assert False != True
  59. # test and/or/not
  60. assert True and True
  61. assert not (True and False)
  62. assert True or True
  63. assert True or False
  64. assert not False
  65. assert not (not True)
  66. assert bool(0) == False
  67. assert bool(1) == True
  68. assert bool([]) == False
  69. assert bool("abc") == True
  70. assert bool([1,2]) == True
  71. assert bool('') == False
  72. # generate assert test for str
  73. assert 'testing' == 'test' + 'ing'
  74. assert 'testing' != 'test' + 'ing2'
  75. assert 'testing' < 'test' + 'ing2'
  76. assert 'testing5' > 'test' + 'ing1'
  77. # test + *=
  78. assert 'abc' + 'def' == 'abcdef'
  79. assert 'abc' * 3 == 'abcabcabc'
  80. # generate assert test for list
  81. assert [1, 2, 3] == [1, 2, 3]
  82. assert [1, 2, 3] != [1, 2, 4]
  83. # test + *=
  84. assert [1, 2, 3] + [4, 5, 6] == [1, 2, 3, 4, 5, 6]
  85. assert [1, 2, 3] * 3 == [1, 2, 3, 1, 2, 3, 1, 2, 3]
  86. # test ?:
  87. a = 5
  88. assert ((a > 3) ? 1 : 0) == 1
  89. assert ((a < 3) ? 1 : 0) == 0
  90. assert eq(round(3.1415926, 2), 3.14)
  91. assert eq(round(3.1415926, 3), 3.142)
  92. assert eq(round(3.1415926, 4), 3.1416)
  93. assert eq(round(-3.1415926, 2), -3.14)
  94. assert eq(round(-3.1415926, 3), -3.142)
  95. assert eq(round(-3.1415926, 4), -3.1416)
  96. assert round(23.2) == 23
  97. assert round(23.8) == 24
  98. assert round(-23.2) == -23
  99. assert round(-23.8) == -24
  100. assert 7**21 == 558545864083284007
  101. assert 2**60 == 1152921504606846976
  102. assert -2**60 == -1152921504606846976
  103. assert eq(2**-2, 0.25)
  104. assert 0**0 == 1
  105. assert 0**1 == 0
  106. assert 1**0 == 1