|
| 1 | +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Tests for coords.""" |
| 16 | +from __future__ import absolute_import |
| 17 | +from __future__ import division |
| 18 | +from __future__ import print_function |
| 19 | + |
| 20 | +import tensorflow as tf # pylint: disable=g-bad-import-order |
| 21 | + |
| 22 | +import coords |
| 23 | +import numpy |
| 24 | +import utils_test |
| 25 | + |
| 26 | +tf.logging.set_verbosity(tf.logging.ERROR) |
| 27 | + |
| 28 | + |
| 29 | +class TestCoords(utils_test.MiniGoUnitTest): |
| 30 | + |
| 31 | + def test_upperleft(self): |
| 32 | + self.assertEqual(coords.from_sgf('aa'), (0, 0)) |
| 33 | + self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 0), (0, 0)) |
| 34 | + self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'A9'), (0, 0)) |
| 35 | + self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (1, 9)), (0, 0)) |
| 36 | + |
| 37 | + self.assertEqual(coords.to_sgf((0, 0)), 'aa') |
| 38 | + self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 0)), 0) |
| 39 | + self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (0, 0)), 'A9') |
| 40 | + self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (0, 0)), (1, 9)) |
| 41 | + |
| 42 | + def test_topleft(self): |
| 43 | + self.assertEqual(coords.from_sgf('ia'), (0, 8)) |
| 44 | + self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 8), (0, 8)) |
| 45 | + self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'J9'), (0, 8)) |
| 46 | + self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (9, 9)), (0, 8)) |
| 47 | + |
| 48 | + self.assertEqual(coords.to_sgf((0, 8)), 'ia') |
| 49 | + self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 8)), 8) |
| 50 | + self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (0, 8)), 'J9') |
| 51 | + self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (0, 8)), (9, 9)) |
| 52 | + |
| 53 | + def test_pass(self): |
| 54 | + self.assertEqual(coords.from_sgf(''), None) |
| 55 | + self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 81), None) |
| 56 | + self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'pass'), None) |
| 57 | + self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (0, 0)), None) |
| 58 | + |
| 59 | + self.assertEqual(coords.to_sgf(None), '') |
| 60 | + self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, None), 81) |
| 61 | + self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, None), 'pass') |
| 62 | + self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, None), (0, 0)) |
| 63 | + |
| 64 | + def test_parsing_9x9(self): |
| 65 | + self.assertEqual(coords.from_sgf('aa'), (0, 0)) |
| 66 | + self.assertEqual(coords.from_sgf('ac'), (2, 0)) |
| 67 | + self.assertEqual(coords.from_sgf('ca'), (0, 2)) |
| 68 | + self.assertEqual(coords.from_sgf(''), None) |
| 69 | + self.assertEqual(coords.to_sgf(None), '') |
| 70 | + self.assertEqual('aa', coords.to_sgf(coords.from_sgf('aa'))) |
| 71 | + self.assertEqual('sa', coords.to_sgf(coords.from_sgf('sa'))) |
| 72 | + self.assertEqual((1, 17), coords.from_sgf(coords.to_sgf((1, 17)))) |
| 73 | + self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'A1'), (8, 0)) |
| 74 | + self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'A9'), (0, 0)) |
| 75 | + self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'C2'), (7, 2)) |
| 76 | + self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'J2'), (7, 8)) |
| 77 | + self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (1, 1)), (8, 0)) |
| 78 | + self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (1, 9)), (0, 0)) |
| 79 | + self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (3, 2)), (7, 2)) |
| 80 | + self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (8, 0)), (1, 1)) |
| 81 | + self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (0, 0)), (1, 9)) |
| 82 | + self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (7, 2)), (3, 2)) |
| 83 | + |
| 84 | + self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (0, 8)), 'J9') |
| 85 | + self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (8, 0)), 'A1') |
| 86 | + |
| 87 | + def test_flatten(self): |
| 88 | + self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 0)), 0) |
| 89 | + self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 3)), 3) |
| 90 | + self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (3, 0)), 27) |
| 91 | + self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 27), (3, 0)) |
| 92 | + self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 10), (1, 1)) |
| 93 | + self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 80), (8, 8)) |
| 94 | + self.assertEqual(coords.to_flat( |
| 95 | + utils_test.BOARD_SIZE, coords.from_flat(utils_test.BOARD_SIZE, 10)), 10) |
| 96 | + self.assertEqual(coords.from_flat( |
| 97 | + utils_test.BOARD_SIZE, coords.to_flat( |
| 98 | + utils_test.BOARD_SIZE, (5, 4))), (5, 4)) |
| 99 | + |
| 100 | + def test_from_flat_ndindex_equivalence(self): |
| 101 | + ndindices = list(numpy.ndindex( |
| 102 | + utils_test.BOARD_SIZE, utils_test.BOARD_SIZE)) |
| 103 | + flat_coords = list(range( |
| 104 | + utils_test.BOARD_SIZE * utils_test.BOARD_SIZE)) |
| 105 | + def _from_flat(flat_coords): |
| 106 | + return coords.from_flat(utils_test.BOARD_SIZE, flat_coords) |
| 107 | + self.assertEqual( |
| 108 | + list(map(_from_flat, flat_coords)), ndindices) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + tf.test.main() |
0 commit comments