/ tests / test_bitmap_c_interface.py
test_bitmap_c_interface.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2019 Matt Land
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  """
 23  `adafruit_imageload.tests.test_bitmap_c_interface`
 24  ====================================================
 25  
 26  These tests are to validate the displayio_shared_bindings classes that other tests are built on.
 27  
 28  * Author(s):  Matt Land
 29  
 30  """
 31  from unittest import TestCase
 32  from .displayio_shared_bindings import Bitmap_C_Interface
 33  
 34  
 35  class TestBitmap_C_Interface(TestCase):
 36      def test_init(self):
 37          b = Bitmap_C_Interface(2, 4, 1)
 38          self.assertEqual(2, b.width)
 39          self.assertEqual(4, b.height)
 40          self.assertEqual(1, b.colors)
 41  
 42      def test_abs(self):
 43          b = Bitmap_C_Interface(5, 2, 1)
 44          self.assertEqual(9, b._abs_pos(4, 1))
 45  
 46      def test_set_tuple(self):
 47          b = Bitmap_C_Interface(2, 4, 1)
 48          b[1, 3] = 67
 49          self.assertEqual(b[1, 3], 67)
 50  
 51      def test_set_abs(self):
 52          b = Bitmap_C_Interface(2, 4, 1)
 53          b[0] = 42
 54          self.assertEqual(b[0], 42)
 55  
 56      def test_abs_and_tuple(self):
 57          b = Bitmap_C_Interface(2, 4, 1)
 58          b[7] = 101
 59          self.assertEqual(101, b[1, 3])
 60  
 61      def test_non_zero(self):
 62          b = Bitmap_C_Interface(2, 4, 1)
 63          b[1, 1] = 100
 64          self.assertEqual(100, b[1, 1])
 65  
 66      def test_throws_x_out_of_range(self):
 67          b = Bitmap_C_Interface(2, 4, 1)
 68          try:
 69              b[2, 1] = 100
 70              self.fail("should have thrown")
 71          except ValueError:
 72              pass
 73  
 74      def test_max(self):
 75          b = Bitmap_C_Interface(2, 4, 1)
 76          b[1, 1] = 66
 77          self.assertEqual(66, b[1, 1])
 78  
 79      def test_uninitialized(self):
 80          b = Bitmap_C_Interface(2, 4, 1)
 81          try:
 82              b[1, 1]
 83              self.fail("should have thrown")
 84          except RuntimeError:
 85              pass
 86  
 87      def test_validate_throws(self):
 88          b = Bitmap_C_Interface(2, 4, 1)
 89          try:
 90              b.validate()
 91          except ValueError:
 92              pass
 93  
 94      def test_repr(self):
 95          b = Bitmap_C_Interface(3, 2, 1)
 96          b[0, 0] = 1
 97          b[1, 0] = 0
 98          b[2, 0] = 0
 99          b[0, 1] = 1
100          b[1, 1] = 1
101          b[2, 1] = 0
102          self.assertEqual("\n   1   0   0\n   1   1   0\n", str(b))
103  
104      def test_decode(self):
105          b = Bitmap_C_Interface(4, 4, 1)
106          self.assertEqual((0, 0), b._decode(0))
107          encoded = b._abs_pos(3, 3)
108          self.assertEqual((3, 3), b._decode(encoded))