<commit_before>import testing_log
import importlib
import testing
import pytest
import rstem.gpio as g
import time
from functools import wraps

'''
Automatic tests of gpio module via loopback output to input

Short GPIO 23 to 24.
'''

OUTPUT_PIN = 23
INPUT_PIN = 24

def io_setup(button=False, input_active_low=False, output_active_low=False, pull=None):
    def decorator(func):
        @wraps(func)
        def wrapper():
            # Setup
            if button:
                i = g.Button(INPUT_PIN)
            else:
                i = g.Input(INPUT_PIN, active_low=input_active_low, pull=pull)
            o = g.Output(OUTPUT_PIN, active_low=output_active_low)

            passed = func(i, o)

            # Teardown
            g.Input(OUTPUT_PIN)
            return passed
        return wrapper
    return decorator

@testing.automatic
@io_setup()
def output_starts_off(i, o):
    return i.is_off()

@testing.automatic
@io_setup()
def output_turned_on(i, o):
    o.off()
    o.on()
    return i.is_on()

@testing.automatic
@io_setup()
def output_turned_off(i, o):
    o.on()
    o.off()
    return i.is_off()

@testing.automatic
@io_setup()
def output_turned_on_via_set(i, o):
    o.off()
    o.level = 1
    return i.is_on()

@testing.automatic
@io_setup()
def output_turned_off_via_set(i, o):
    o.on()
    o.level = 0
    return i.is_off()

@testing.automatic
@io_setup()
def io_on_off_sequence(i, o):
    on_times = 0
    off_times = 0
    TRIES = 100
    for n in range(TRIES):
        o.on()
        if i.is_on:
            on_times += 1
        o.off()
        if i.is_off:
            off_times += 1
    passed = on_times == TRIES and off_times == TRIES
    if not passed:
        print("Failed: on_times {}, off_times {}, TRIES {}".format(on_times, off_times, TRIES))
    return passed

@testing.automatic
@io_setup()
def output_init_start_off_false(i, o):
    return False

@testing.automatic
@io_setup(output_active_low=True)
def output_init_active_low_on(i, o):
    o.on()
    return i.level == 0

@testing.automatic
@io_setup(output_active_low=True)
def output_init_active_low_off(i, o):
    o.off()
    return i.level == 1

@testing.automatic
@io_setup()
def input_is_off(i, o):
    o.off()
    return i.is_off()

@testing.automatic
@io_setup()
def input_is_on(i, o):
    o.off()
    return i.is_off()

@testing.automatic
@io_setup()
def input_is_off_via_get(i, o):
    o.on()
    return i.level

@testing.automatic
@io_setup()
def input_is_on_via_get(i, o):
    o.off()
    return not i.level

@testing.automatic
@io_setup()
def input_wait_for_change(i, o):
    return False

@testing.automatic
@io_setup()
def input_wait_for_change_with_timeout(i, o):
    return False

@testing.automatic
@io_setup()
def input_call_if_changed(i, o):
    return False

@testing.automatic
@io_setup()
def input_call_if_changed_disable(i, o):
    return False

@testing.automatic
@io_setup(input_active_low=True)
def input_init_active_low_on(i, o):
    o.level = 0
    return i.is_on()

@testing.automatic
@io_setup(input_active_low=True)
def input_init_active_low_off(i, o):
    o.level = 1
    return i.is_off()

@testing.automatic
@io_setup()
def input_init_poll_period_slow(i, o):
    return False

@testing.automatic
@io_setup()
def input_init_pullup(i, o):
    return False

@testing.automatic
@io_setup()
def input_init_pulldown(i, o):
    return False

@testing.automatic
@io_setup()
def input_init_pullnone(i, o):
    return False

@testing.automatic
def input_invalid_pin():
    passed = False
    try:
        Input(5)
    except ValueError:
        passed = True
    return passed

@testing.automatic
@io_setup(button=True)
def input_button_is_pressed(b, o):
    o.on()
    return b.is_pressed()

@testing.automatic
@io_setup(button=True)
def input_button_is_released(b, o):
    o.off()
    return b.is_released()

@testing.automatic
@io_setup()<commit_msg>gpio - testing - passed 3 more tests<commit_after>import testing_log
import importlib
import testing
import pytest
import rstem.gpio as g
import time
from functools import wraps

'''
Automatic tests of gpio module via loopback output to input

Short GPIO 23 to 24.
'''

OUTPUT_PIN = 23
INPUT_PIN = 24

def io_setup(button=False, input_active_low=False, output_active_low=False, pull=None):
    def decorator(func):
        @wraps(func)
        def wrapper():
            # Setup
            if button:
                i = g.Button(INPUT_PIN)
            else:
                i = g.Input(INPUT_PIN, active_low=input_active_low, pull=pull)
            o = g.Output(OUTPUT_PIN, active_low=output_active_low)

            passed = func(i, o)

            # Teardown
            g.Input(OUTPUT_PIN)
            return passed
        return wrapper
    return decorator

@testing.automatic
@io_setup()
def output_starts_off(i, o):
    return i.is_off()

@testing.automatic
@io_setup()
def output_turned_on(i, o):
    o.off()
    o.on()
    return i.is_on()

@testing.automatic
@io_setup()
def output_turned_off(i, o):
    o.on()
    o.off()
    return i.is_off()

@testing.automatic
@io_setup()
def output_turned_on_via_set(i, o):
    o.off()
    o.level = 1
    return i.is_on()

@testing.automatic
@io_setup()
def output_turned_off_via_set(i, o):
    o.on()
    o.level = 0
    return i.is_off()

@testing.automatic
@io_setup()
def io_on_off_sequence(i, o):
    on_times = 0
    off_times = 0
    TRIES = 100
    for n in range(TRIES):
        o.on()
        if i.is_on:
            on_times += 1
        o.off()
        if i.is_off:
            off_times += 1
    passed = on_times == TRIES and off_times == TRIES
    if not passed:
        print("Failed: on_times {}, off_times {}, TRIES {}".format(on_times, off_times, TRIES))
    return passed

@testing.automatic
@io_setup()
def output_init_start_off_false(i, o):
    return False

@testing.automatic
@io_setup(output_active_low=True)
def output_init_active_low_on(i, o):
    o.on()
    return i.level == 0

@testing.automatic
@io_setup(output_active_low=True)
def output_init_active_low_off(i, o):
    o.off()
    return i.level == 1

@testing.automatic
@io_setup()
def input_is_off(i, o):
    o.off()
    return i.is_off()

@testing.automatic
@io_setup()
def input_is_on(i, o):
    o.off()
    return i.is_off()

@testing.automatic
@io_setup()
def input_is_off_via_get(i, o):
    o.on()
    return i.level

@testing.automatic
@io_setup()
def input_is_on_via_get(i, o):
    o.off()
    return not i.level

@testing.automatic
@io_setup()
def input_wait_for_change(i, o):
    return False

@testing.automatic
@io_setup()
def input_wait_for_change_with_timeout(i, o):
    return False

@testing.automatic
@io_setup()
def input_call_if_changed(i, o):
    return False

@testing.automatic
@io_setup()
def input_call_if_changed_disable(i, o):
    return False

@testing.automatic
@io_setup(input_active_low=True)
def input_init_active_low_on(i, o):
    o.level = 0
    return i.is_on()

@testing.automatic
@io_setup(input_active_low=True)
def input_init_active_low_off(i, o):
    o.level = 1
    return i.is_off()

@testing.automatic
@io_setup()
def input_init_poll_period_slow(i, o):
    return False

@testing.automatic
@io_setup()
def input_init_pullup(i, o):
    return False

@testing.automatic
@io_setup()
def input_init_pulldown(i, o):
    return False

@testing.automatic
@io_setup()
def input_init_pullnone(i, o):
    return False

@testing.automatic
def input_invalid_pin():
    passed = False
    try:
        g.Input(5)
    except ValueError:
        passed = True
    return passed

@testing.automatic
@io_setup(button=True)
def input_button_is_pressed(b, o):
    o.level = 0
    return b.is_pressed()

@testing.automatic
@io_setup(button=True)
def input_button_is_released(b, o):
    o.level = 1
    return b.is_released()

@testing.automatic
@io_setup()
{'fraction_non_alphanumeric': 0.1137762955361724, 'fraction_numerical': 0.006028732683427399, 'mean_word_length': 3.2723287671232875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3306210', 'n_tokens_mistral': 2963, 'n_tokens_neox': 2845, 'n_words': 650}