def test_seek_tell(): """Seek in a file. """ with open("seek.txt", "w") as fout: fout.write("12345678") assert fout.tell() == 8 with open("seek.txt", "r") as fin: # Offset relative to the beginning of the file. assert fin.seek(1) == 1 assert fin.tell() == 1 assert fin.read(1) == "2" assert fin.seek(1, 0) == 1 assert fin.read(1) == "2" # Offset relative to the current cursor position. assert fin.seek(1, 1) == 3 assert fin.read(1) == "4" # Offset relative to the end of the file. assert fin.seek(-1, 2) == 7 assert fin.read(1) == "8" assert fin.tell() == 8 # Seek outside of the file. with assert_raises(OSError): fin.seek(-1) with assert_raises(OSError): fin.seek(1, 2)
def test_bad_arguments(): poll = select.poll() with assert_raises(TypeError, "channel object required"): poll.register(None) with assert_raises(OSError): poll.unregister(None)
def test_bad_arguments(): # Bad socket family. with assert_raises(OSError): socket.socket(-1) # Bad socket type. with assert_raises(OSError): socket.socket(socket.AF_INET, -1)
def test_bad_arguments(): queue = Queue() with assert_raises(TypeError, "can't convert NoneType to int"): queue.read(None) with assert_raises(TypeError, "object with buffer protocol required"): queue.write(None)
def test_system(): os.system('kernel/thrd/list') with assert_raises(OSError, "Command not found: '1/2/3'"): os.system('1/2/3') with assert_raises(OSError, "Command failed with -1"): os.system('')
def test_bad_arguments(): event = Event() with assert_raises(TypeError, "can't convert NoneType to int"): event.read(None) with assert_raises(TypeError, "can't convert NoneType to int"): event.write(None)
def test_bad_arguments(): # Too many devices. with assert_raises(ValueError, "too many devices"): Dac([0, 1, 2]) # Bad devices type. with assert_raises(TypeError, "bad devices"): Dac(None)
def test_errors(): # Failed accept. socket_stub.set_accept(-1) listener = socket.socket() listener.bind(("192.168.0.1", 8080)) listener.listen(1) with assert_raises(OSError, 'socket accept failed'): listener.accept() listener.close() # Failed bind and close. socket_stub.set_bind(-1) socket_stub.set_close(-1) listener = socket.socket() with assert_raises(OSError, 'socket bind failed'): listener.bind(("192.168.0.1", 8080)) with assert_raises(OSError, 'socket close failed'): listener.close() # Failed listen. socket_stub.set_listen(-1) listener = socket.socket() listener.bind(("192.168.0.1", 8080)) with assert_raises(OSError, 'socket listen failed'): listener.listen(1) listener.close() # Failed connect. socket_stub.set_connect(-1) sock = socket.socket() with assert_raises(OSError, 'socket connect failed'): sock.connect(("192.168.0.1", 8080)) sock.close() # Failed send and recv. socket_stub.set_send([-1, 0]) socket_stub.set_recv([-1, 0]) sock = socket.socket() sock.connect(("192.168.0.1", 8080)) assert sock.send(b'bar') == 0 assert sock.recv(5) == b'' assert sock.send(b'bar') == 0 assert sock.recv(5) == b'' sock.close() assert socket_stub.reset_failed() == 0
def test_bad_arguments(): # Bad socket family. with assert_raises(OSError): socket.socket(-1) # Bad socket type. with assert_raises(OSError): socket.socket(socket.AF_INET, -1) assert socket_stub.reset_failed() == 0
def test_bad_arguments(): # Too long tuple. with assert_raises(TypeError, "expected tuple of length 2"): Timer((1, 0, 0), None, 1) # Bad timeout arguement. with assert_raises(TypeError, "can't convert NoneType to float"): Timer(None, None, 1) # Wrong type of second argument. with assert_raises(TypeError, "expected <class 'Event'>"): Timer(1, 1, 1)
def test_read_write_fail(): # The driver fails to read and write data. with assert_raises(OSError): block = 512 * b'1' SD.write_block(0, block) with assert_raises(OSError): SD.read_block(0) with assert_raises(OSError): buf = bytearray(512) SD.read_block_into(0, buf)
def test_bad_arguments(): # Too many devices. with assert_raises(ValueError, "too many devices"): Dac([0, 1, 2]) # Bad devices type. with assert_raises(TypeError, "bad devices"): Dac(None) # Bad pin. with assert_raises(ValueError, "bad pin"): Dac(board.PIN_LED)
def test_directory(): assert os.getcwd() == '/fs' with assert_raises(NotImplementedError): os.chdir('dir') if fs == 'fat16': os.mkdir('dir') with assert_raises(NotImplementedError): os.rename('dir', 'dir2') with assert_raises(NotImplementedError): os.rmdir('dir2')
def test_bad_arguments(): # Bad device. with assert_raises(ValueError, "bad device"): Can(100, Can.SPEED_500KBPS) can = Can(board.CAN_0, Can.SPEED_500KBPS) can.start() # Bad id. with assert_raises(ValueError, "bad frame id"): can.write(-3, b'') # Bad frame data length. with assert_raises(ValueError, "bad frame data length"): can.write(1, b'123456789')
def test_assert_raises(): with assert_raises(RuntimeError, "expected error"): raise RuntimeError("expected error") try: with assert_raises(TypeError): raise ValueError("unexpected error") except ValueError as e: assert str(e) == "unexpected error" try: with assert_raises(TypeError): pass except ExceptionNotRaisedError as e: assert str(e) == "<class 'TypeError'> not raised"
def test_bad_arguments(): # Bad spi object. with assert_raises(TypeError): Sd(None) # Bad block size. with assert_raises(ValueError, "bad buffer length"): SD.write_block(0, '') with assert_raises(ValueError, "bad buffer length"): SD.write_block(0, 1024 * ' ') with assert_raises(TypeError): SD.read_block_into(0, ' ') # Read fails. with assert_raises(OSError): SD.read_block(0)
def test_bad_arguments(): # Bad mode. with assert_raises(ValueError, "bad pin mode 3"): Pin(board.PIN_LED, 3) # bad device. with assert_raises(ValueError, "bad pin device -1"): Pin(-1, Pin.OUTPUT) with assert_raises(ValueError, "bad pin device 500"): Pin(500, Pin.OUTPUT) led = Pin(board.PIN_LED, Pin.OUTPUT) with assert_raises(ValueError, "bad pin value 2"): led.write(2)
def test_missing_file(): """Try to open a non-existing file. """ with assert_raises(OSError): open("missing.txt", "r")
def test_bad_arguments(): # Bad device. with assert_raises(ValueError, "bad device"): Adc(67, 0) # Bad pin device. with assert_raises(ValueError, "bad pin device"): Adc(board.ADC_0, -5) # Bad reference. with assert_raises(ValueError, "bad reference"): Adc(board.ADC_0, PIN_A0, -1) # Bad sampling rate. with assert_raises(ValueError, "bad sampling rate"): Adc(board.ADC_0, PIN_A0, Adc.REFERENCE_VCC, 0)
def test_bad_arguments(): # Bad device. with assert_raises(ValueError, "bad device"): I2C(100) i2c = I2C(0) i2c.start() # Bad buffer length in read. with assert_raises(ValueError, "bad buffer length"): buf = bytearray(1) i2c.read_into(0, buf, 2) # Bad buffer length in write. with assert_raises(ValueError, "bad buffer length"): buf = bytearray(1) i2c.write(0, buf, 2)
def test_flush(): """Flush a file. """ with assert_raises(NotImplementedError, "file_obj_flush"): with open("flush.txt", "w") as fout: fout.write('') fout.flush()
def test_read_write(): """Test the read + write mode. """ with open("rw.txt", "w+") as f: f.write("foo") f.seek(0) assert f.read() == "foo" with open("rw.txt", "r") as fin: assert fin.read() == "foo" with open("rw.txt", "r") as fin: with assert_raises(OSError): fin.write('foo') with open("rw.txt", "w") as fout: with assert_raises(OSError): fout.read()
def test_bad_arguments(): # Bad device. with assert_raises(ValueError, "bad device"): Spi(100, 0) # Bad slave select. with assert_raises(ValueError, "bad slave select"): Spi(board.SPI_0, -1) # Bad mode. with assert_raises(ValueError, "bad mode"): Spi(board.SPI_0, board.PIN_D3, -1) # Bad polarity. with assert_raises(ValueError, "bad polarity"): Spi(board.SPI_0, board.PIN_D3, Spi.MODE_MASTER, 1, -1) # Bad phase. with assert_raises(ValueError, "bad phase"): Spi(board.SPI_0, board.PIN_D3, Spi.MODE_MASTER, 1, 1, -1)
def test_format(): """Format the file system. """ with assert_raises(OSError): os.format("apa") try: os.format("/fs") except: print("Failed to format /fs.")
def test_bad_arguments(): event = Event() queue = Queue() # Bad device. with assert_raises(ValueError, "bad exti device -1"): Exti(-1, Exti.BOTH, event, 1) # Bad trigger. with assert_raises(ValueError, "bad trigger -1"): Exti(board.EXTI_D3, -1, event, 1) # Bad channel. with assert_raises(TypeError, "expected <class 'Event'> or <class 'Queue'>"): Exti(board.EXTI_D3, Exti.BOTH, None, 1) # Bad event data. with assert_raises(TypeError, "can't convert NoneType to int"): Exti(board.EXTI_D3, Exti.BOTH, event, None, 1) # Bad queue data. with assert_raises(TypeError, "object with buffer protocol required"): Exti(board.EXTI_D3, Exti.BOTH, queue, None, 1) # Bad callback. with assert_raises(TypeError, "bad callback"): Exti(board.EXTI_D3, Exti.BOTH, event, 1, 1)
def test_bad_arguments(): event = Event() # Bad device. with assert_raises(ValueError, "bad exti device -1"): Exti(-1, Exti.BOTH, event, 1) # Bad trigger. with assert_raises(ValueError, "bad trigger -1"): Exti(board.EXTI_D3, -1, event, 1) # Bad event channel. with assert_raises(TypeError, "expected <class 'Event'>"): Exti(board.EXTI_D3, Exti.BOTH, None, 1) # Bad event mask. with assert_raises(TypeError, "can't convert NoneType to int"): Exti(board.EXTI_D3, Exti.BOTH, event, None, 1) # Bad callback. with assert_raises(TypeError, "bad callback"): Exti(board.EXTI_D3, Exti.BOTH, event, 1, 1)
def test_register_unregister(): poll = select.poll() queue = Queue() event = Event() poll.register(queue) poll.register(event) poll.unregister(queue) poll.unregister(event) with assert_raises(OSError): poll.unregister(queue)
def test_register_unregister(): poll = select.poll() queue = Queue() event = Event() can = Can(board.CAN_0) poll.register(queue) poll.register(event) poll.register(can) poll.unregister(queue) poll.unregister(event) poll.unregister(can) with assert_raises(OSError): poll.unregister(queue)
def test_set_mode(): led = Pin(board.PIN_LED, Pin.INPUT) led.set_mode(Pin.OUTPUT) with assert_raises(ValueError, "bad pin mode 6"): led.set_mode(6)
def test_smoke(): """Various tests. """ help() objs = [ array, binascii, cmath, collections, hashlib, io, json, math, os, random, socket, struct, sys, time, zlib, gc, micropython, kernel, sync, drivers, board, sync.Event, drivers.Pin, kernel.Timer, other ] try: objs.append(drivers.Exti) except: pass try: objs.append(drivers.Dac) except: pass try: objs.append(drivers.Spi) except: pass for obj in objs: print() help(obj) try: import foo except: pass print("dir:", dir()) print("sys.platform:", sys.platform) print("os.uname:", os.uname()) print("time.time:", time.time()) print("time.localtime():", time.localtime()) print("time.localtime():", time.localtime(1475271052)) print('time.localtime().tm_year:', time.localtime().tm_year) time.sleep(0.1) time.sleep_ms(1) time.sleep_us(1) try: print('CWD:', os.getcwd()) except OSError as e: print(e) try: os.mkdir('foo') except Exception as e: print(e) with assert_raises(NotImplementedError): os.chdir('foo') with assert_raises(NotImplementedError): os.chdir('..') with assert_raises(NotImplementedError): os.rename('foo', 'bar') with assert_raises(NotImplementedError): os.rmdir('bar') assert other.foo() == True with assert_raises(OSError): os.system("bad") print(os.system("kernel/thrd/list")) sio = io.StringIO("foo") sio.seek(0, 2) print("bar", file=sio) sio.seek(0) assert sio.read().strip() == "foobar" print(cmath.phase(complex(-1.0, 0.0))) z = complex(-1.0, 0.0) assert z == z.real + z.imag * 1j print(cmath.cos(math.pi)) ordered_dict = collections.OrderedDict([(1,"a")]) print(ordered_dict.popitem()) m = hashlib.sha256() m.update(b"Nobody inspects") m.update(b" the spammish repetition") print(m.digest()) kernel.sys_lock() kernel.sys_unlock() if os.uname().machine != "Linux with Linux": print('Collecting the garbage...') gc.collect() if hasattr(kernel, 'thrd_yield'): kernel.thrd_yield() with assert_raises(NotImplementedError): kernel.thrd_join(None) thrd = kernel.thrd_self() with assert_raises(NotImplementedError): kernel.thrd_set_name('foo') print('thrd_get_name(): ', kernel.thrd_get_name()) assert kernel.thrd_get_by_name('main') == thrd with assert_raises(OSError): kernel.thrd_get_by_name('foo') kernel.thrd_set_log_mask(thrd, 0xff) assert kernel.thrd_get_log_mask() == 0xff prio = kernel.thrd_get_prio() kernel.thrd_set_prio(thrd, prio + 1) assert kernel.thrd_get_prio() == prio + 1 with assert_raises(NotImplementedError): kernel.thrd_set_global_env('foo', 'bar') print('thrd_get_global_env(CWD): ', kernel.thrd_get_global_env('CWD')) with assert_raises(NotImplementedError): kernel.thrd_set_env('foo', 'bar') print('thrd_get_env(CWD): ', kernel.thrd_get_env('CWD'))
def test_bad_arguments(): # Owi object expected. with assert_raises(TypeError, "Owi object expected"): Ds18b20(None)
def test_bad_arguments(): # Bad pin device. with assert_raises(ValueError, "bad pin device"): Owi(-1)
def test_smoke(): """Various tests. """ help() objs = [ array, binascii, cmath, collections, hashlib, io, json, math, os, random, socket, struct, sys, time, zlib, gc, micropython, kernel, sync, drivers, board, sync.Event, drivers.Pin, kernel.Timer, other ] try: objs.append(drivers.Exti) except: pass try: objs.append(drivers.Dac) except: pass try: objs.append(drivers.Spi) except: pass for obj in objs: print() help(obj) try: import foo except: pass print("dir:", dir()) print("sys.platform:", sys.platform) print("os.uname:", os.uname()) print("time.time:", time.time()) print("time.localtime():", time.localtime()) print("time.localtime():", time.localtime(1475271052)) print('time.localtime().tm_year:', time.localtime().tm_year) time.sleep(0.1) time.sleep_ms(1) time.sleep_us(1) try: print('CWD:', os.getcwd()) except OSError as e: print(e) try: os.mkdir('foo') except Exception as e: print(e) with assert_raises(NotImplementedError): os.chdir('foo') with assert_raises(NotImplementedError): os.chdir('..') with assert_raises(NotImplementedError): os.rename('foo', 'bar') with assert_raises(NotImplementedError): os.rmdir('bar') assert other.foo() == True with assert_raises(OSError): os.system("bad") print(os.system("kernel/thrd/list")) sio = io.StringIO("foo") sio.seek(0, 2) print("bar", file=sio) sio.seek(0) assert sio.read().strip() == "foobar" print(cmath.phase(complex(-1.0, 0.0))) z = complex(-1.0, 0.0) assert z == z.real + z.imag * 1j print(cmath.cos(math.pi)) ordered_dict = collections.OrderedDict([(1, "a")]) print(ordered_dict.popitem()) m = hashlib.sha256() m.update(b"Nobody inspects") m.update(b" the spammish repetition") print(m.digest()) kernel.sys_lock() kernel.sys_unlock() if os.uname().machine != "Linux with Linux": print('Collecting the garbage...') gc.collect() if hasattr(kernel, 'thrd_yield'): kernel.thrd_yield() with assert_raises(NotImplementedError): kernel.thrd_join(None) thrd = kernel.thrd_self() with assert_raises(NotImplementedError): kernel.thrd_set_name('foo') print('thrd_get_name(): ', kernel.thrd_get_name()) assert kernel.thrd_get_by_name('main') == thrd with assert_raises(OSError): kernel.thrd_get_by_name('foo') kernel.thrd_set_log_mask(thrd, 0xff) assert kernel.thrd_get_log_mask() == 0xff prio = kernel.thrd_get_prio() kernel.thrd_set_prio(thrd, prio + 1) assert kernel.thrd_get_prio() == prio + 1 with assert_raises(NotImplementedError): kernel.thrd_set_global_env('foo', 'bar') print('thrd_get_global_env(CWD): ', kernel.thrd_get_global_env('CWD')) with assert_raises(NotImplementedError): kernel.thrd_set_env('foo', 'bar') print('thrd_get_env(CWD): ', kernel.thrd_get_env('CWD'))