<commit_before>
""" Module to test example graphs. """

from config import Config
from tests import test
from graph import Node, Edge, Graph
from wire import Wire
from resistor import Resistor
from common_filters import InvertingOpAmpFilter
from opamp import Opamp

def test_wire():
    # Simple wire
    graph = Graph()
    node_a = Node(graph, value=5, fixed=True)
    node_b = Node(graph, output=True)
    wire = Wire(graph, node_a, node_b)
    graph.add_component(wire)
    graph.solve()
    # Test that the voltage at both points is equal
    test(5, node_b.value(), epsilon=Config.epsilon)

def test_chained_wires():
    # Chained wires
    graph = Graph()
    node_a = Node(graph, value=10, fixed=True)
    node_b = Node(graph)
    node_c = Node(graph)
    node_d = Node(graph, output=True)

    wire = Wire(graph, node_a, node_b)
    graph.add_component(wire)

    wire = Wire(graph, node_b, node_c)
    graph.add_component(wire)

    wire = Wire(graph, node_c, node_d)
    graph.add_component(wire)

    graph.solve()

    # Test that the voltage at both points is equal
    test(10, node_d.value(), epsilon=Config.epsilon)

def test_resistor():
    """ Test graphs. """
    # Simple resistor
    graph = Graph()
    node_a = Node(graph, value=5, fixed=True)
    node_b = Node(graph, value=0, fixed=True)
    edge_i = Edge(graph, node_a, node_b)
    resistor = Resistor(graph, 10, node_a, node_b, edge_i)
    graph.add_component(resistor)
    graph.solve()
    # Test that the current through the resistor is 0.5
    test(0.5, edge_i.value(), epsilon=Config.epsilon)

def test_buffer():
    # Buffer
    graph = Graph()
    node_out = Node(graph, output=True)
    node_in = Node(graph, value=5, fixed=True)
    op_amp = Opamp(graph, node_a=node_out, node_b=node_in, node_out=node_out)
    graph.add_component(op_amp)
    graph.solve()
    test(5, node_out.value(), epsilon=Config.epsilon)

def test_inverting_opamp():
    # Inverting op amp
    filt = InvertingOpAmpFilter.make(100, 100)
    input_signal = [5]
    output_signal = filt.execute(input_signal)
    # Test that the op amp inverts the input voltage
    test(-input_signal[0], output_signal[0], epsilon=Config.epsilon)

def test_all():
    test_wire()
    test_chained_wires()
    test_resistor()
    test_buffer()
    test_inverting_opamp()

if __name__ == "__main__":
    test_all()<commit_msg>Added final result to tests<commit_after>
""" Module to test example graphs. """

from config import Config
from tests import test
from graph import Node, Edge, Graph
from wire import Wire
from resistor import Resistor
from common_filters import InvertingOpAmpFilter
from opamp import Opamp

def test_wire():
    # Simple wire
    graph = Graph()
    node_a = Node(graph, value=5, fixed=True)
    node_b = Node(graph, output=True)
    wire = Wire(graph, node_a, node_b)
    graph.add_component(wire)
    graph.solve()
    # Test that the voltage at both points is equal
    return test(5, node_b.value(), epsilon=Config.epsilon)

def test_chained_wires():
    # Chained wires
    graph = Graph()
    node_a = Node(graph, value=10, fixed=True)
    node_b = Node(graph)
    node_c = Node(graph)
    node_d = Node(graph, output=True)

    wire = Wire(graph, node_a, node_b)
    graph.add_component(wire)

    wire = Wire(graph, node_b, node_c)
    graph.add_component(wire)

    wire = Wire(graph, node_c, node_d)
    graph.add_component(wire)

    graph.solve()

    # Test that the voltage at both points is equal
    return test(10, node_d.value(), epsilon=Config.epsilon)

def test_resistor():
    """ Test graphs. """
    # Simple resistor
    graph = Graph()
    node_a = Node(graph, value=5, fixed=True)
    node_b = Node(graph, value=0, fixed=True)
    edge_i = Edge(graph, node_a, node_b)
    resistor = Resistor(graph, 10, node_a, node_b, edge_i)
    graph.add_component(resistor)
    graph.solve()
    # Test that the current through the resistor is 0.5
    return test(0.5, edge_i.value(), epsilon=Config.epsilon)

def test_buffer():
    # Buffer
    graph = Graph()
    node_out = Node(graph, output=True)
    node_in = Node(graph, value=5, fixed=True)
    op_amp = Opamp(graph, node_a=node_out, node_b=node_in, node_out=node_out)
    graph.add_component(op_amp)
    graph.solve()
    return test(5, node_out.value(), epsilon=Config.epsilon)

def test_inverting_opamp():
    # Inverting op amp
    filt = InvertingOpAmpFilter.make(100, 100)
    input_signal = [5]
    output_signal = filt.execute(input_signal)
    # Test that the op amp inverts the input voltage
    return test(-input_signal[0], output_signal[0], epsilon=Config.epsilon)

def test_all():
    tests = [test_wire, test_chained_wires, test_resistor, test_buffer,
             test_inverting_opamp]
    if False in [t() for t in tests]:
        print("At least one test failed.")
    else:
        print("All tests passed.")

if __name__ == "__main__":
    test_all()
{'fraction_non_alphanumeric': 0.10982070089649552, 'fraction_numerical': 0.010187449062754686, 'mean_word_length': 3.62241054613936, 'pattern_counts': {'":': 2, '<': 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': '3985623', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1663, 'n_words': 444}