Esempio n. 1
0
def pruebas_grafo_un_elemento():
    grafo = Grafo()
    var = "iron"
    grafo.add(var)
    print_test("Pruebas Grafo, el grafo no es None", grafo is not None)
    print_test("Pruebas Grafo, la cantidad de elementos es uno",
               len(grafo) == 1)
    print_test("Pruebas Grafo, el grafo contiene a 'var'", var in grafo)
    print_test("Pruebas Grafo, la lista de claves del grafo tiene largo uno",
               len(grafo.keys()) == 1)
    print_test(
        "Pruebas Grafo, la lista de claves del grafo tiene el elemento 'var'",
        var in grafo.keys())
    print_test("Pruebas Grafo, la lista de adyacentes de 'var' esta vacia",
               not grafo.adyacentes(var))

    print_exception(grafo.borrar_arista, KeyError,
                    "Pruebas Grafo, borrar arista lanza KeyError", var,
                    "maiden")
    print_exception(grafo.obtener_peso_arista, KeyError,
                    "Pruebas Grafo, obtener peso arista lanza KeyError", var,
                    "maiden")
    print_exception(grafo.adyacentes, KeyError,
                    "Pruebas Grafo, obtener adyacentes lanza KeyError", "test")

    del grafo[var]
    print_test(
        "Pruebas Grafo, despues de elminar 'var' la cantidad de elementos es cero",
        len(grafo) == 0)
    print_test(
        "Pruebas Grafo, despues de elminar 'var' el grafo no contiene a 'var'",
        var not in grafo)
    print_test(
        "Pruebas Grafo, despues de elminar 'var' la lista de claves del grafo esta vacia",
        not grafo.keys())
    print_exception(
        grafo.__delitem__, KeyError,
        "Pruebas Grafo, despues de elminar 'var' eliminar un elemento lanza KeyError",
        var
    )  # se utiliza __delitem__ directamente para utilizar la funcion 'print_exception', pero es equivalente a 'del grafo["test"]'
    print_exception(
        grafo.adyacentes, KeyError,
        "Pruebas Grafo, despues de elminar 'var' obtener adyacentes lanza KeyError",
        var)
Esempio n. 2
0
def pruebas_grafo_vacio():
    grafo = Grafo()
    print_test("Pruebas Grafo, el grafo no es None", grafo is not None)
    print_test("Pruebas Grafo, la cantidad de elementos es cero",
               len(grafo) == 0)
    print_test("Pruebas Grafo, el grafo no contiene elementos", "test"
               not in grafo)
    print_test("Pruebas Grafo, la lista de claves del grafo esta vacia",
               not grafo.keys())
    print_exception(
        grafo.__delitem__, KeyError,
        "Pruebas Grafo, eliminar un elemento lanza KeyError", "test"
    )  # se utiliza __delitem__ directamente para utilizar la funcion 'print_exception', pero es equivalente a 'del grafo["test"]'
    print_exception(grafo.borrar_arista, KeyError,
                    "Pruebas Grafo, borrar arista lanza KeyError", "test",
                    "test2")
    print_exception(grafo.obtener_peso_arista, KeyError,
                    "Pruebas Grafo, obtener peso arista lanza KeyError",
                    "test", "test2")
    print_exception(grafo.adyacentes, KeyError,
                    "Pruebas Grafo, obtener adyacentes lanza KeyError", "test")
Esempio n. 3
0
def pruebas_grafo_varios_elementos():
    grafo = Grafo()
    vars = ["iron", "maiden", "black", "sabbath"]
    for value in vars:
        grafo.add(value)

    print_test("Pruebas Grafo, la cantidad de elementos es cuatro",
               len(grafo) == 4)
    for value in vars:
        ok = value in grafo
        if not ok: break
        ok2 = value in grafo.keys()
        if not ok2: break

    print_test(
        "Pruebas Grafo, todos los elementos estan contenidos en el grafo", ok)
    print_test(
        "Pruebas Grafo, todos los elementos estan contenidos en el grafo las claves del grafo",
        ok2)

    grafo.agregar_arista(vars[0], vars[1])  #
    grafo.agregar_arista(vars[0], vars[2])  # |0|------|1|
    grafo.agregar_arista(vars[1], vars[2])  #  |  -.    |
    grafo.agregar_arista(vars[1], vars[3])  #  |     -. |
    grafo.agregar_arista(vars[2], vars[3])  # |2|------|3|

    print_test("Pruebas Grafo, la arista existe",
               grafo.obtener_peso_arista(vars[0], vars[1]))
    print_test("Pruebas Grafo, la arista opuesta existe",
               grafo.obtener_peso_arista(vars[1], vars[0]))
    print_test("Pruebas Grafo, la arista existe",
               grafo.obtener_peso_arista(vars[0], vars[2]))
    print_test("Pruebas Grafo, la arista opuesta existe",
               grafo.obtener_peso_arista(vars[2], vars[1]))
    print_test("Pruebas Grafo, la arista existe",
               grafo.obtener_peso_arista(vars[1], vars[2]))
    print_test("Pruebas Grafo, la arista opuesta existe",
               grafo.obtener_peso_arista(vars[2], vars[1]))
    print_test("Pruebas Grafo, la arista existe",
               grafo.obtener_peso_arista(vars[1], vars[3]))
    print_test("Pruebas Grafo, la arista opuesta existe",
               grafo.obtener_peso_arista(vars[3], vars[1]))
    print_test("Pruebas Grafo, la arista existe",
               grafo.obtener_peso_arista(vars[2], vars[3]))
    print_test("Pruebas Grafo, la arista opuesta existe",
               grafo.obtener_peso_arista(vars[3], vars[2]))

    print_test("Pruebas Grafo, adyacentes ok",
               all(x in [vars[1], vars[2]] for x in grafo.adyacentes(vars[0])))
    print_test(
        "Pruebas Grafo, adyacentes ok",
        all(x in [vars[0], vars[2], vars[3]]
            for x in grafo.adyacentes(vars[1])))
    print_test(
        "Pruebas Grafo, adyacentes ok",
        all(x in [vars[0], vars[1], vars[3]]
            for x in grafo.adyacentes(vars[2])))
    print_test("Pruebas Grafo, adyacentes ok",
               all(x in [vars[1], vars[2]] for x in grafo.adyacentes(vars[3])))

    for vertice1 in grafo:  # borro todas las aristas
        for vertice2 in grafo:
            try:
                grafo.borrar_arista(vertice1, vertice2)
            except ValueError:
                pass

    print_test("Pruebas Grafo, la cantidad de elementos es cuatro",
               len(grafo) == 4)

    grafo.agregar_arista(vars[0], vars[1])  #
    grafo.agregar_arista(vars[0], vars[2])  # |0|------|1|
    grafo.agregar_arista(vars[1], vars[2])  #  |  -.    |
    grafo.agregar_arista(vars[1], vars[3])  #  |     -. |
    grafo.agregar_arista(vars[2], vars[3])  # |2|------|3|

    del grafo[vars[0]]
    print_test("Pruebas Grafo, el nodo borrado no pertenece al grafo",
               not vars[0] in grafo)
    print_exception(
        grafo.obtener_peso_arista, KeyError,
        "Pruebas Grafo, obtener peso arista borrada lanza KeyError", vars[0],
        vars[1])
    print_exception(
        grafo.obtener_peso_arista, KeyError,
        "Pruebas Grafo, obtener peso arista reciproca borrada lanza KeyError",
        vars[1], vars[0])
    print_test("Pruebas Grafo, la cantidad de elementos es tres",
               len(grafo) == 3)
    print_test(
        "Pruebas Grafo, la cantidad de elementos de grafo.keys() es tres",
        len(grafo.keys()) == 3)

    print_test("Pruebas Grafo, adyacentes ok", vars[0]
               not in grafo.adyacentes(vars[1]))
    print_test("Pruebas Grafo, adyacentes ok", vars[0]
               not in grafo.adyacentes(vars[2]))
    print_test("Pruebas Grafo, adyacentes ok", vars[0]
               not in grafo.adyacentes(vars[3]))