Description2019-04-13 IMPORTANT UPDATE: A bug in MEASURE() means all previous results are garbage (backwards)
Run unit tests in SmileBASIC!
Executes all *.TEST files in the current project.
The default output is "TTY:" (console screen). To write to a file, change _LOGFILE$ on line 2 to "TXT:<Filename>" or call SETOUTPUT "TXT:<filename>","NUL:"Instructions'set up common resources
DIM RNDINT%[10000]
VAR _I%
FOR _I% = 0 TO 9999
RNDINT%[_I%] = RND(&h7FFFffff) - &h3FFFffff
NEXT
'logging
VAR _LOGFILE1$ = "TTY:"
VAR _LOGFILE2$ = "NUL:"
VAR _TBUFFER$ = ""
COMMON DEF PRINTLN STRING$
VAR TYPE$ = LEFT$(_LOGFILE1$,4)
IF TYPE$ == "TTY:" THEN
PRINT STRING$
ELSEIF TYPE$ == "TXT:" THEN
INC _TBUFFER$, STRING$
INC _TBUFFER$, CHR$(10)
ENDIF
TYPE$ = LEFT$(_LOGFILE2$,4)
IF TYPE$ == "TTY:" THEN
PRINT STRING$
ELSEIF TYPE$ == "TXT:" THEN
INC _TBUFFER$, STRING$
INC _TBUFFER$, CHR$(10)
ENDIF
END
COMMON DEF FLUSH_LOG
VAR TYPE$ = LEFT$(_LOGFILE1$,4)
IF TYPE$ == "TXT:" THEN
PRINT "Output written to "+LOGFILE1$
SAVE _LOGFILE1$, _TBUFFER$
ENDIF
TYPE$ = LEFT$(_LOGFILE2$,4)
IF TYPE$ == "TXT:" THEN
PRINT "Output written to "+LOGFILE2$
SAVE _LOGFILE2$, _TBUFFER$
ENDIF
END
VAR NUM_SUCCESSFUL%
VAR NUM_TESTS%
DEF NEW_TEST
NUM_SUCCESSFUL% = 0
NUM_TESTS% = 0
END
COMMON DEF ASSERT BOOL%, DESCRIPTION$, TIME%
INC NUM_TESTS%
IF BOOL% THEN
COLOR #TLIME
INC DESCRIPTION$, " PASS in "
INC NUM_SUCCESSFUL%
ELSE
COLOR #TRED
INC DESCRIPTION$, " FAIL in "
ENDIF
INC DESCRIPTION$, STR$(TIME%)
INC DESCRIPTION$, "ms"
PRINTLN DESCRIPTION$
COLOR #TWHITE
END
DEF GENERATE TEST$, BEFORE$, SLOT%
PRGEDIT SLOT%
PRGDEL -1
PRGINS "COMMON DEF _TEST()"
PRGINS " " + BEFORE$
PRGINS " VAR I%=0"
PRGINS " M%=MILLISEC+1"
PRGINS " WHILE MILLISEC+1==M%WEND"
PRGINS " WHILE MILLISEC<M%+1000"
PRGINS " " + TEST$
PRGINS " INC I%"
PRGINS " WEND"
PRGINS " RETURN I%"
PRGINS "END"
EXEC SLOT%
END
'Approximate speed of contents of FN$ (w/o overhead) in nanoseconds
COMMON DEF MEASURE(FN$, BEFORE$)
'establish baseline measurement
GENERATE "'"*LEN(FN$), BEFORE$, 3
VAR BASELINE% = _TEST()
'measure target code
GENERATE FN$, BEFORE$, 3
VAR ITERATIONS% = _TEST()
'return ns/iteration
RETURN 1e6/ITERATIONS% - 1e6/BASELINE%
END
COMMON DEF COMPARE_SPEED FNS$[], BEFORE$
IF LEN(FNS$) < 1 THEN ?"Too few elements in function list":STOP ENDIF
DIM TIMES#[LEN(FNS$)]
VAR J%
FOR J% = LEN(FNS$) - 1 TO 0 STEP -1
TIMES#[J%] = MEASURE(FNS$[J%], BEFORE$)
NEXT
RSORT TIMES#, FNS$
COLOR #TLIME
PRINTLN FORMAT$("%-24S: %Fns",POP(FNS$),POP(TIMES#))
COLOR #TWHITE
WHILE LEN(TIMES#) > 1
PRINTLN FORMAT$("%-24S: %Fns",POP(FNS$),POP(TIMES#))
WEND
IF LEN(TIMES#) == 1 THEN
COLOR #TRED
PRINTLN FORMAT$("%-24S: %Fns",POP(FNS$),POP(TIMES#))
COLOR #TWHITE
ENDIF
END
'run tests
VAR _TESTTIME%
DIM TESTS$[0]
FILES "TXT:", TESTS$
FOR _I% = LEN(TESTS$) - 1 TO 0 STEP -1
VAR TESTFILE$ = MID$(TESTS$[_I%],1,32)
IF RIGHT$(TESTFILE$,5) != ".TEST" THEN CONTINUE
NEW_TEST
PRINTLN "=== RUNNING " + TESTFILE$ + " ==="
_TESTTIME% = MILLISEC
EXEC "PRG1:"+TESTFILE$
ASSERT NUM_SUCCESSFUL%==NUM_TESTS%, FORMAT$(" * %D of %D",NUM_SUCCESSFUL%,NUM_TESTS%), MILLISEC - _TESTTIME%
NEXT
FLUSH_LOGNotesOPTION STRICT
VAR TIME%
DIM ARR%[10]
VAR INT%, FLT#, STN$
TIME% = MILLISEC
INT% = 11
ASSERT INT%==11, "Integer Assignment", MILLISEC-TIME%
TIME% = MILLISEC
FLT# = 11.0000001
ASSERT FLT#==11.0000001, "Float Assignment", MILLISEC-TIME%
TIME% = MILLISEC
STN$ = "11"
ASSERT STN$=="11", "String Assignment", MILLISEC-TIME%
TIME% = MILLISEC
ARR%[5] = 11
ASSERT ARR%[5]==11, "Subscript Assignment", MILLISEC-TIME%COMMON DEF PLUS
A = A + 1
END
COMMON DEF NEGATEDSUBTRACT
A = A -- 1
END
COMMON DEF INCREMENT
INC A
END
COMMON DEF EXPLICITINCREMENT
INC A,1
END
COMMON DEF NEGATEDDECREMENT
DEC A,-1
END
COMMON DEF _ADD_INIT
A = 0
END
DIM FNS$[5]
FNS$[0]="PLUS"
FNS$[1]="NEGATEDSUBTRACT"
FNS$[2]="INCREMENT"
FNS$[3]="EXPLICITINCREMENT"
FNS$[4]="NEGATEDDECREMENT"
COMPARE_SPEED FNS$, "_ADD_INIT"
Documentation
Running ympe-test will search for and run all program files with the ".TEST" prefix in the current directory.
This section summarizes the library routines provided for tests.
Set the output files to use when logging. Prefixes:
NUL: - No output. Either, both, or neither output file may be "NUL:", in which case that channel will not be written to.
TTY: - The terminal screen. Either, both, or neither output file may be "TTY:", in which case that output will be written to the text console.
TXT: - A text file. The filename should be specified (e.g. "TXT:RESULTS.LOG"). Either, both, or neither output file may be "TXT:<filename>", in which case that output will be buffered and written to the file specified when FLUSH_BUFFER is called.
Flush any TXT: file outputs specified. (This will result in a save dialog prompt for each output directed to a file). FLUSH_BUFFER is called automatically by ympe-test after all tests have run, but may be useful for storing information prior to crash.
Record the result of a test BOOL%. DESCRIPTION$ is a description of the test, used in the log. TIME% is the time in milliseconds to complete a command, if the output of a command is being verified. Passing a number < 0 will be treated as if no timing information available.
Measure the speed of a list of functions FNS$ and output the result for comparison. If BEFORE_FN$ is not the empty string ("") then the function specified in BEFORE_FN$ will be called prior to measuring each function in FNS$.
3 Comment(s)Y____Head AdminGardeningI like to garden!HobbiesReadingI like to read books!HobbiesDrawingI like to draw!HobbiesY____Head AdminGardeningI like to garden!HobbiesReadingI like to read books!HobbiesDrawingI like to draw!HobbiesThis is WRONG!
correct results are:
12Me23Deep SleepHiddenWebsiteIntermediate ProgrammerI can make programs, but I still have trouble here and there. Programming StrengthThat evidence is clearly FAKE your honor!