CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

132 lines
5.3 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2020 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Small suite of tests for CAT-SOOP's check module
  18. """
  19. import math
  20. import random
  21. import unittest
  22. from .. import check
  23. from ..test import CATSOOPTest
  24. # -----------------------------------------------------------------------------
  25. class TestCheckers(CATSOOPTest):
  26. def test_equal(self):
  27. for pair in ((1, 1.0), (2, 2), (False, 0)):
  28. self.assertTrue(check.equal()(*pair))
  29. for pair in ((1, 1.000001), (2, 3), (False, None)):
  30. self.assertFalse(check.equal()(*pair))
  31. def test_check_result(self):
  32. for pair in ((1, 1.0), (2, 2), (False, 0)):
  33. pair = [{"result": i, "details": {}} for i in pair]
  34. self.assertTrue(check.check_result(check.equal())(*pair))
  35. self.assertTrue(check.check_result()(*pair))
  36. for pair in ((1, 1.000001), (2, 3), (False, None)):
  37. pair = [{"result": i, "details": {}} for i in pair]
  38. self.assertFalse(check.check_result(check.equal())(*pair))
  39. def test_number_close(self):
  40. for i in range(50):
  41. threshold = random.uniform(1e-6, 1e-2)
  42. x = random.uniform(-1000, 1000)
  43. y = x + threshold - 1e-9
  44. self.assertTrue(check.number_close(threshold)(x, y))
  45. y = x - threshold + 1e-9
  46. self.assertTrue(check.number_close(threshold)(x, y))
  47. for i in range(50):
  48. threshold = random.uniform(1e-6, 1e-2)
  49. x = random.uniform(-1000, 1000)
  50. y = x - threshold + 1e-1
  51. self.assertFalse(check.number_close(threshold)(x, y))
  52. y = x + threshold - 1e-1
  53. self.assertFalse(check.number_close(threshold)(x, y))
  54. for i in range(50):
  55. threshold = random.uniform(1e-6, 1e-2)
  56. x = complex(random.uniform(-1000, 1000), random.uniform(-1000, 1000))
  57. y = x + (threshold - 1e-9) * math.e ** (1j * random.uniform(0, 2 * math.pi))
  58. self.assertTrue(check.number_close(threshold)(x, y))
  59. y = x + (threshold + 1e-9) * math.e ** (1j * random.uniform(0, 2 * math.pi))
  60. self.assertFalse(check.number_close(threshold)(x, y))
  61. self.assertTrue(check.number_close(1e-9, 0.01)(1e-10, 0))
  62. self.assertFalse(check.number_close(None, 0.01)(1e-10, 0))
  63. self.assertTrue(check.number_close(1e-9, 0.01)(100000, 100100))
  64. self.assertFalse(check.number_close(1e-9, 0.01)(100000, 10100.0001))
  65. def test_evaled(self):
  66. vals = (1, 1e-9, "cat", None, True, False, 1 + 2j)
  67. for ix, i in enumerate(vals):
  68. self.assertTrue(check.evaled(check.equal())(repr(i), repr(i)))
  69. if ix != 0:
  70. self.assertFalse(
  71. check.evaled(check.equal())(repr(i), repr(vals[ix - 1]))
  72. )
  73. for i in range(2):
  74. self.assertTrue(
  75. check.evaled(check.number_close(1e-6))(repr(i), repr(i + 1e-9))
  76. )
  77. self.assertFalse(
  78. check.evaled(check.number_close(1e-6))(repr(i), repr(i + 1e-5))
  79. )
  80. self.assertFalse(check.evaled()('"cat"', '"cat'))
  81. self.assertFalse(check.evaled()('"cat', '"cat"'))
  82. self.assertFalse(check.evaled()("[1, 2, 3]", "1, 2, 3]"))
  83. self.assertTrue(check.evaled()("[1, 2, 3]", "[1, 2, 3]"))
  84. def test_list_all(self):
  85. x = [1, 1e-9, "cat", None, True, False, 1.2j]
  86. y = x[:]
  87. self.assertTrue(check.list_all()(x, y))
  88. self.assertFalse(check.list_all()(x, y[:-1]))
  89. self.assertFalse(check.list_all()(x, y + ["something"]))
  90. self.assertFalse(check.list_all()(x, y[::-1]))
  91. def test_list_all_unordered(self):
  92. x = [1, 1e-9, "cat", None, True, False, 1.2j]
  93. y = x[:]
  94. self.assertTrue(check.list_all_unordered()(x, y))
  95. self.assertFalse(check.list_all_unordered()(x, y[:-1]))
  96. self.assertFalse(check.list_all_unordered()(x, y + ["something"]))
  97. self.assertTrue(check.list_all_unordered()(x, y[::-1]))
  98. self.assertFalse(check.list_all_unordered()(x, y[::-1] + [y[-1]]))
  99. def test_dict_all(self):
  100. a = {"cat": [7, 8, 9], "coca": "cola", "thing": lambda x: x ** 3}
  101. b = {k: v for k, v in reversed(list(a.items()))}
  102. self.assertTrue(check.dict_all()(a, b))
  103. a = {"cat": 7, "dog": 1e-9, "ferret": 1 + 2j, "tomato": 3j}
  104. b = {k: v + random.uniform(-1e-6, 1e-6) for k, v in a.items()}
  105. self.assertTrue(check.dict_all(check.number_close(1e-6))(a, b))
  106. b = {k: v + 1j * random.uniform(1e-2, 1e-1) for k, v in a.items()}
  107. self.assertFalse(check.dict_all(check.number_close(1e-6))(a, b))
  108. if __name__ == "__main__":
  109. unittest.main()