tests: add cmake ctest infrastructure so `make test` can run
[contrib/mraa.git] / tests / gpio_checks.py
1 #!/usr/bin/env python
2
3 # Author: Costin Constantin <costin.c.constantin@intel.com>
4 # Copyright (c) 2015 Intel Corporation.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 import mraa as m
26 import unittest as u
27 import os, re, sys
28 from time import sleep
29
30 MRAA_GPIO = 3
31
32 @u.skipIf(m.pinModeTest(MRAA_GPIO, m.PIN_GPIO) != True, str(MRAA_GPIO) + "is not a valid Gpio on this platform")
33 class GpioChecks(u.TestCase):
34
35   def setUp(self):
36     self.pin = m.Gpio(MRAA_GPIO)
37     self.gpio_path = "/sys/class/gpio/gpio" + str(self.pin.getPin(True))
38
39   def tearDown(self):
40     # dereference pin to force cleanup
41     self.pin = ""
42
43   def test_returning_pin_no(self):
44     self.pin_no = self.pin.getPin() # i should have the pin no. here as set when initing Gpio class
45     self.assertEqual(self.pin_no, MRAA_GPIO, "Something wrong happened ... set pin doesn't correspond to retrieved one")
46
47   def test_returning_pin_as_on_sys(self):
48     self.assertTrue(os.path.exists(self.gpio_path))
49
50   def test_set_GPIO_as_output(self):
51     self.pin.dir(m.DIR_OUT)
52     dir_file = open(self.gpio_path + "/direction")
53     dir_file_content = dir_file.readline()[:-1]
54     dir_file.close()
55     self.assertMultiLineEqual(dir_file_content, "out")
56
57   def test_set_GPIO_as_input(self):
58     self.pin.dir(m.DIR_IN)
59     dir_file = open(self.gpio_path + "/direction")
60     dir_file_content = dir_file.readline()[:-1]
61     dir_file.close()
62     self.assertMultiLineEqual(dir_file_content, "in")
63
64   def test_GPIO_as_output_write_HIGH_level(self):
65     self.pin.dir(m.DIR_OUT)
66     self.pin.write(1)
67     val_file = open(self.gpio_path + "/value")
68     sysfs_pin_value = val_file.readline()[:-1]
69     val_file.close()
70     self.assertEqual(int(sysfs_pin_value),1, "Value doesn't match the HIGH state")
71
72   def test_GPIO_as_output_write_LOW_level(self):
73     self.pin.dir(m.DIR_OUT)
74     self.pin.write(0)
75     val_file = open(self.gpio_path + "/value")
76     sysfs_pin_value = val_file.readline()[:-1]
77     val_file.close()
78     self.assertEqual(int(sysfs_pin_value), 0, "Value doesn't match the LOW state")
79
80   def test_GPIO_as_input_and_write_HIGH_on_it(self):
81     self.pin.dir(m.DIR_IN)
82     res = self.pin.write(1)
83     self.assertNotEqual(res, m.SUCCESS, "The command should fail")
84
85   def test_GPIO_as_input_and_write_LOW_on_it(self):
86     self.pin.dir(m.DIR_IN)
87     res = self.pin.write(0)
88     self.assertGreater(res, 0, "The command should have returned value greater than 0")
89
90 if __name__ == '__main__':
91   u.main()