написал язычок и компилятор, поскольку это модно и молодежно.
пример теста(форматирование частично слетело, как уж есть):
//some tests fot compiler
definition Tests
fun testAll()
end Tests
//##########################
module Tests
import
SYSTEM
Strings
Con = Console
IO
Suite = TestHelper
alias
assert = TestHelper.assert
error = TestHelper.error
write = Con.write
writeLn = Con.writeLn
writeInt= Con.writeDec
OK = TestHelper.ok
//test type constructors
type
WeekDay = enum (one,two,three)
Colors = enum (White, Red, Blue, Green, Black)
Record2d = record var _x,_y:int end
SetOfColors = setof (Colors)
FuncType = fun (int,int):int
//############
var //global vars
Res:int
lint:int
//###########################
//func OK() { Con.writeLn("..OK") }
fun testLocalPointer():bool {
const Val = 777;
let lx=0;
var lp:^int = addr(lx);
lp^=Val;
return lx==Val
}
fun testEnum():bool {
const cc = WeekDay.two
var lday:WeekDay=WeekDay.two;
write("testEnum..")
case lday
of WeekDay.two : return true
else error("wrong case selected 1")
..
case lday
of cc : return true
else error("wrong case selected 2")
..
return false
}
fun testArray():bool {
return true
}
fun testRecord():bool {
return true
}
fun testTypes(){
Suite.test(testLocalPointer, "testLocalPointer")
Suite.test(testEnum,"test enum")
Suite.test(testArray,"test array")
Suite.test(testRecord,"test record")
}
//###############################
//test statemens
//###############################
fun testRepeat(fx:int) {
write("Test repeat...")
repeat
if fx%100==0 { write("*")}
until --fx == 0
OK()
}
fun testLoop(floops:int) {
let lx = 0;
let lloops=floops;
write("testLoop..")
while floops>0 do
++lx
floops-=1
..
TestHelper.assert(lx==lloops,"error in testLoop")
//test real loop
lx=1000
loop
lx-=1
if lx==0 do break ..
..
OK()
}
fun testIf(){}
fun testWhile(){}
fun testCase() {
var lx:int=5;
write("testCase..")
case lx
of 1,1+1,1+2: error("1")
of 4: error("2")
of 5: writeLn("case 5 selected") //must be selected
of 12: error("3")
else error("4")
..
OK()
}
fun testFinally() {
write("Test finally..")
return
finally
write("FINALLY passed correctly")
OK()
}
fun testStatements(){
testRepeat(1000)
testLoop(1000)
testIf()
testWhile()
testCase()
testFinally()
}
//################################
//################################
fun logOpsTest() {
let
lt=true
lf=false;
write("logOperationTest..")
assert(not (lt and lf), "boolean and error")
assert(lt or lf, "boolean or error")
OK()
}
//########
var Depth:int=0
fun
incr(i:int):int {return i+1}
decr(i:int):int {return i-1}
fun testRecursion(fx:int) {
write("Recursion..")
write("*")
Depth = incr(Depth) //Depth+=1
if fx>0 { testRecursion(fx-1) }
Depth = decr(Depth) //Depth-=1
if Depth==0 {
write("out")
}
//OK()
}
fun testMemory(fsize:int) {
precond fsize>0;
var lptr: address = nil;
write("TEST MEMORY..")
lptr = SYSTEM.allocate(fsize)
write("allocated..")
SYSTEM.free(lptr)
write("deallocated..")
OK()
}
fun testIndex() {
var ls:seq(char,10);
let i=0;
write("testIndex(must be 'alex 1234')..")
ls[i]='a'
ls[++i]='l'
ls[++i]='e'
ls[++i]='x'
ls[++i]=' '
ls[++i]='1'
ls[++i]='2'
ls[++i]='3'
ls[++i]='4'
ls[++i]=char(0)
write(ls)
OK()
}
fun testVarParam(var fvar:int) {
write("Test VAR param(value must be 777)..");
write("variable parameter is ");
writeInt(fvar);
OK();
}
fun testFiles() {
var
lf :IO.File
i,lsize :int
lok :bool
fun printCh(ff:IO.File){ //read char and print it
precond ff # IO.NilFile;
Console.writeChar(char(IO.fread(ff)))
}
do
lf=IO.fopen("test.als") //open this file
assert(lf#IO.NilFile,"cannot open file")
lsize=IO.fsize(lf) //get file size
i=0
while i<lsize { //print all the file to console
printCh(lf)
i+=1
}
finally //finally close the file
lok = IO.fclose(lf) //close the file
if not lok do error("cannot close file")..
writeLn("file closed")
}
//Test file creation and writing
fun testCreateFile() {
let
i=100
lfile = IO.fcreate("xxx.txt")
lok = false;
//var lfile:IO.File;
assert(lfile#IO.NilFile,"cannot create file")
while i>0 do
lok = IO.fwrite(lfile,'X')
--i
..
lok = IO.fclose(lfile)
}
/*
func testFileStat()
var
lf:IO.File
lok:bool
lstat:IO.FileInfo
do
lf = IO.fopen("test.als")
lok = IO.finfo(lf, lstat)
if (not lok) Error("cannot get file statistic") ..
finally
lok = IO.fclose(lf)
..testFileStat
*/
cluster MyGroup
const
c1=100
c2=200
c3=300
type
LocalInt = int
//func One():int return 1 ..One
end MyGroup
@testGroup(){
let lx = MyGroup.c1;
write("TEST GROUP..")
writeInt(lx) write(",")
writeInt(MyGroup.c2) write(",")
writeInt(MyGroup.c3)
OK()
}
//module TestFunctionType
@FF1(in fs:Strings.String) = writeLn(fs) ..
@FF2(i,j:int)= ..
type ffuu = @(int,int)
/*
func testFunctionType()
var
lf :func(int,int)
lfs :func(in Strings.String)
lff :func(int):func(int) //function return function
do
lf = FF2
lfs = FF1
//lfs("CALLING FORMAL FUNCTION")
//lff = FF2
//lfs = actualFunction
//lfs("Formal Function Go")
..testFunctionType
*/
//end module
fun failPrecond(fval :int) {
precond fval<10 //this precondition must be failed by caller
raise TestHelper.TestError
}
fun testPrecond():bool {
//write("precondition test..")
try
failPrecond(100)
catch SYSTEM.PrecondEx {
return true
//writeLn("OK-precondition failed as needed")
else
return false
//writeLn("ERROR - precondition failed WRONG")
}
}
fun testTrueFalse():bool {
var lb,lres:bool;
//write("testTrueFalse..")
return not (true and false) or (true or false)
}
fun inout(in fin:Record2d, out fout:Record2d) {
fout = fin
}
/*
func testInOutParams()
do
end testInOutParams
*/
fun testStringConstantInit() {
var lb:seq(char,30)="TestingString";
write("testStringConst..")
Con.print (lb)
writeLn(" - if text is 'TestingString' then OK")
OK()
}
fun testAll() {
writeLn("#nModule Tests STARTED:")
testTypes()
testStatements()
testIndex()
//testEnum()
testRecursion(20)
testMemory(1000)
testRepeat(1000)
lint = 777
testVarParam(var lint)
testGroup()
// testFiles() //Program exited with return code: 0
// T.testCreateFile()
// T.testFileStat()
logOpsTest()
Suite.test(testPrecond,"test precondition")
Suite.test(testTrueFalse, "test boolean expressions")
writeLn("END OF TESTS")
}
do
//testAll()
//Con.writeLn(" Daddy is very big popa")
end Tests.