Skip to content

Instantly share code, notes, and snippets.

@ramsesoriginal
Last active January 15, 2018 15:03
Show Gist options
  • Save ramsesoriginal/e954dd0f5fd4c9f83fec3a203f8b3081 to your computer and use it in GitHub Desktop.
Save ramsesoriginal/e954dd0f5fd4c9f83fec3a203f8b3081 to your computer and use it in GitHub Desktop.
PA09 Test
import time
outputBuffer = ""
output = ""
def out(s = False, l = False, trimList=True):
global outputBuffer
if s:
outputBuffer = outputBuffer + s
if l:
sl = str(l)
if trimList:
sl = (sl[:18] + ' ... ' + sl[-17:]) if len(sl) > 40 else sl
if hasattr(l, '__len__'):
sl = str(len(l)) + ": " + sl
outputBuffer = outputBuffer + " " + sl
outputBuffer = outputBuffer + "\n"
def resetOutputBuffer():
global outputBuffer
outputBuffer = "\n"
def flush():
global outputBuffer
global output
output = output + outputBuffer
def resetOut():
global output
output = "\n"
resetOutputBuffer()
def printOut():
global output
print(output)
def singleTest(titel = "Test", liste = [15,0,8,1,2,10,11,4,6,14,12,-1,15,16], vergleicher = (lambda a, b: a <= b), delayCmp=False, delaySwp=False, testResult=True, pOk = False, externalTest= (lambda: True), externalTestResult="Fehler bei externem Test", debugPrintOnSwap=False, copyList=True):
ok = True
if copyList:
liste = liste[:]
def test_cmp(i, j):
if delayCmp:
time.sleep(delayCmp)
return vergleicher(liste[i], liste[j])
def test_swp(i, j):
if delaySwp:
time.sleep(delaySwp)
if debugPrintOnSwap:
print(liste)
liste[i], liste[j] = liste[j], liste[i]
if debugPrintOnSwap:
print(liste)
input()
out(titel, liste)
try:
quicksort(len(liste),test_cmp,test_swp)
except Exception as ex:
out(" [ERROR]", ex)
ok = False
if testResult:
i = 0
while ok and i < len(liste)-2:
if not (vergleicher(liste[i], liste[i+1]) or not vergleicher(liste[i+1], liste[i])):
out(" Fehler beim Vergleich von '" + str(liste[i]) + "' an stelle " + str(i) + " mit '" + str(liste[i+1]) + "' an stelle " + str(i+1))
ok = False
i += 1
elif not externalTest():
out(" " + externalTestResult)
ok = False
if not ok:
out(" [FAIL]: Liste ist nicht sortiert", liste, False)
else:
out(" [OK]")
if not ok or pOk:
flush()
resetOutputBuffer()
return ok
def test(printOK = False, stopOnError = False):
print("\n")
ok = True
resetOut()
if ok or not stopOnError:
ok = singleTest(
titel = "Sehr Einfach Liste vorwaerts sortiert",
liste= [9,0,5,1,2,7,8,3,4,6],
debugPrintOnSwap= False,
pOk = printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel = "Liste vorwaerts sortiert",
pOk = printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "Leere liste sortiert",
liste= [],
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "n = 1",
liste= [0],
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "n = 2, schon sortiert",
liste= [0, 1],
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "n = 2, nicht sortiert",
liste= [1, 0],
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "n = 3",
liste= [1,0,2],
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "Alles 0",
liste= [0] * 100
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "Rueckwaerts Sortieren",
vergleicher= lambda a,b: a > b,
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "Gerade vs Ungerade",
vergleicher= lambda a,b: a%2 < b%2,
liste= list(range(-50,51)),
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "Lange Liste",
liste= list(range(-4**4,(4**4) + 1)),
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "Alles Langsam",
liste= list(range(50,60))+list(range(100,60,-2))+list(range(0,50,2)),
delayCmp= 0.001,
delaySwp= 0.001,
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "Swap sehr langsam",
liste= list(range(50,60))+list(range(100,60,-2))+list(range(0,50,2)),
delaySwp= 0.05,
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "Buchstabenliste, case dependent",
liste= list("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern"),
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
ok = singleTest(
titel= "Buchstabenliste, case indipendent",
liste= list("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern"),
vergleicher = (lambda a, b: a.upper() <= b.upper()),
pOk=printOK
)
print('.', end='', flush=True)
if ok or not stopOnError:
liste = [9,0,5,1,2,7,8,3,4,6]
allowedValues={9,0,5}
pivot=5
stillChecking = True
FirstRun = True
newPivot = False
isQuicksort = True
ext_IsQuicksort = lambda: isQuicksort
def check_quicksort_cmp(a, b):
nonlocal liste
nonlocal pivot
nonlocal allowedValues
nonlocal stillChecking
nonlocal FirstRun
nonlocal newPivot
nonlocal isQuicksort
i = liste.index(a)
j = liste.index(b)
if not isQuicksort:
return True
if stillChecking:
if a not in allowedValues or b not in allowedValues:
if pivot not in {a, b}:
isQuicksort = False
if newPivot:
pivot = sorted([liste[0], liste[1], liste[2]])[1]
newPivot = False
if FirstRun and (i==9 or j==9):
allowedValues = {liste[0], liste[1], liste[2]}
newPivot = True
FirstRun = False
elif (i==4 or j==4):
stillChecking = False
return a <= b
ok = singleTest(
titel= "Test ob es quicksort ist",
liste= liste,
vergleicher= check_quicksort_cmp,
testResult = False,
externalTest = ext_IsQuicksort,
externalTestResult = "Kein Quicksort",
copyList = False,
debugPrintOnSwap= False,
pOk=printOK
)
if ok:
out("ALL OK! Woohoo!")
else:
out("There are issues to resolve.")
flush()
printOut()
test(printOK = True, stopOnError = True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment