Queer European MD passionate about IT

test_project.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import pytest
  2. from project import get_coin_to_flip, get_parameters, get_parity, is_power_of_two, solve
  3. def main():
  4. test_get_coin_to_flip()
  5. test_get_parameters()
  6. test_get_parity()
  7. test_is_power_of_two()
  8. test_solve()
  9. def test_get_coin_to_flip():
  10. for i in (4, 8, 16):
  11. initial_state, key_position, _ = get_parameters(board_side=i)
  12. c = get_coin_to_flip(initial_state=initial_state, key_position=key_position)
  13. assert 0 <= c < i**2
  14. final_state = solve(initial_state, c)
  15. assert get_parity(final_state) == key_position
  16. def test_get_parameters():
  17. for i in (4, 8, 16):
  18. initial_state, key_position, coin_to_flip = get_parameters(board_side=i)
  19. assert len(initial_state) == i ** 2
  20. assert 0 <= key_position <= 2**i - 1
  21. assert 0 <= coin_to_flip <= 2**i - 1
  22. for i in range(3, 5, 7):
  23. with pytest.raises(ValueError):
  24. get_parameters(board_side=i)
  25. def test_get_parity():
  26. assert get_parity('0100') == 1
  27. assert get_parity('0010') == 2
  28. assert get_parity('0' * 64) == 0
  29. assert get_parity('1' * 64) == 0
  30. assert get_parity('0000'
  31. '0010'
  32. '0000'
  33. '0000') == 6
  34. assert get_parity('00111011'
  35. '00001001'
  36. '10010101'
  37. '11011011'
  38. '01001110'
  39. '01110000'
  40. '10001101'
  41. '11101001') == 17
  42. def test_is_power_of_two():
  43. assert not is_power_of_two(0)
  44. for i in range(1, 13):
  45. assert is_power_of_two(2**i)
  46. assert not is_power_of_two(2 ** i + 3)
  47. def test_solve():
  48. for i in (4, 8, 16):
  49. initial_state, _, coin_to_flip = get_parameters(board_side=i)
  50. final_state = solve(initial_state=initial_state, coin_to_flip=coin_to_flip)
  51. for j, (c0, c1) in enumerate(zip(initial_state, final_state)):
  52. assert j == coin_to_flip or c0 == c1
  53. if __name__ == '__main__':
  54. main()