URL
https://opencores.org/ocsvn/tcp_socket/tcp_socket/trunk
Subversion Repositories tcp_socket
[/] [tcp_socket/] [trunk/] [chips2/] [test_suite/] [test_c2verilog] - Rev 2
Go to most recent revision | Compare with Previous | Blame | View Log
#!/usr/bin/env python
import os
import sys
os.system("python-coverage erase")
def test(test, code, no_init=False):
f = open("test.c", 'w')
f.write(code)
f.close()
if no_init:
result = os.system("python-coverage run -p ../c2verilog iverilog run no_initialize_memory test.c")
else:
result = os.system("python-coverage run -p ../c2verilog iverilog run test.c")
if(os.path.exists("main")):
os.remove("main")
if(os.path.exists("arbiter")):
os.remove("arbiter")
if(os.path.exists("real_main")):
os.remove("real_main")
if result == 0:
print test, "...pass"
else:
print test, "...fail"
sys.exit(0)
if no_init:
result = os.system("python-coverage run -p ../c2verilog iverilog run speed no_initialize_memory test.c")
else:
result = os.system("python-coverage run -p ../c2verilog iverilog run speed test.c")
if(os.path.exists("main")):
os.remove("main")
if(os.path.exists("arbiter")):
os.remove("arbiter")
if(os.path.exists("real_main")):
os.remove("real_main")
if result == 0:
print test, "...pass"
else:
print test, "...fail"
sys.exit(0)
def test_fails(test, code, no_init=False):
f = open("test.c", 'w')
f.write(code)
f.close()
print "One error expected ..."
if no_init:
result = os.system("python-coverage run -p ../c2verilog iverilog run no_initialize_memory test.c")
else:
result = os.system("python-coverage run -p ../c2verilog iverilog run test.c")
if result == 0:
print test, "...fail"
sys.exit(0)
else:
print test, "...pass"
test("unsigned divide 1",
"""
void main(){
unsigned a = 10;
unsigned b = 5;
assert(a/b==2);
}
"""
)
test("long unsigned divide 1",
"""
void main(){
long unsigned a = 1000000L;
long unsigned b = 500000L;
assert(a/b==2);
}
"""
)
test("divide 1",
"""
void main(){
int a = 10;
int b = 5;
assert(a/b==2);
}
"""
)
test("long divide 1",
"""
void main(){
long a = 1000000L;
long b = 500000L;
assert(a/b==2);
}
"""
)
test("long 1",
"""
void main(){
long a = 100000L;
assert(a==100000L);
}
"""
)
test("long 2",
"""
void main(){
long a = 100000L;
assert(sizeof a == 4);
}
"""
)
test("long 3",
"""
void main(){
long a = 0xffffL;
assert(a+1 != 0);
assert(a+1 == 0x10000L);
}
"""
)
test("long 4",
"""
void main(){
long a = 0xffffL;
long b = 0xffffL;
assert(a+b == 0x1fffeL);
}
"""
)
test("long 5",
"""
void main(){
long a = 0xffffffffUL;
long b = 0x0;
assert(a < b);
}
"""
)
test("long 6",
"""
void main(){
unsigned long a = 0xffffffffUL;
long b = 0x0;
assert(a > b);
}
"""
)
test("long 7",
"""
void test_long(long a){
assert(a+1 != 0);
assert(a+1 == 0x10000L);
}
void main(){
long a = 0xffffL;
test_long(a);
}
"""
)
test("long 8",
"""
void test_long(unsigned long a){
assert(a > 0);
}
void main(){
unsigned long a = 0xffffffffUL;
test_long(a);
}
"""
)
test("long 9",
"""
void test_long(long a){
assert(a < 0);
}
void main(){
unsigned long a = 0xffffffffUL;
test_long(a);
}
"""
)
test("long 10",
"""
void test_long(unsigned long a){
assert(a > 0);
}
void main(){
long a = 0xffffffffUL;
test_long(a);
}
"""
)
test("long 11",
"""
void main(){
long a[2];
a[0] = 0xffffffffUL;
a[1] = 0xffffffffUL;
assert(a[1] == 0xffffffffUL);
}
"""
)
test("long 12",
"""
void main(){
long a[2];
int b[2];
a[0] = 1;
b[0] = 2;
a[1] = 3;
b[1] = 4;
assert(a[0] == 1);
assert(b[0] == 2);
assert(a[1] == 3);
assert(b[1] == 4);
}
"""
)
test("void 1",
"""
void func(unsigned i){
}
void main(){
int a = 1;
assert(a++ == 1);
assert(a++ == 2);
assert(a == 3);
assert(a-- == 3);
assert(a-- == 2);
assert(a == 1);
}
"""
)
test("postfix 1",
"""
int main(){
int a = 1;
assert(a++ == 1);
assert(a++ == 2);
assert(a == 3);
assert(a-- == 3);
assert(a-- == 2);
assert(a == 1);
return 0;
}
"""
)
test("sizeof 1",
"""
int main(){
unsigned a = 0xffffU;
unsigned b = 0x7fff;
unsigned c[4];
unsigned d[] = "asdfg";
assert(sizeof a == 2);
assert(sizeof b == 2);
assert(sizeof c == 8);
assert(sizeof d == 12);
return 0;
}
"""
)
test("type_unsigned 1",
"""
int main(){
unsigned a = 0xffffU;
unsigned b = 0x7fff;
assert(a==0xffffU);
assert(a > b);
return 0;
}
"""
)
test("type_unsigned 2",
"""
int main(){
unsigned a = 0xffffU;
unsigned b = 0x7fff;
assert(a==0xffffU);
assert(b < a);
return 0;
}
"""
)
test("type_unsigned 3",
"""
int test(unsigned a){
return a;
}
int main(){
assert(test(3) == 3);
return 0;
}
"""
)
test("type_unsigned 4",
"""
int main(){
int a = 1;
unsigned b = 1;
assert(a == b);
return 0;
}
"""
)
test("type_unsigned 5",
"""
int main(){
int a = 1;
assert(a == 1);
return 0;
}
"""
)
test("type_unsigned 6",
"""
int main(){
int a = 1;
assert(1 == a);
return 0;
}
"""
)
test("type_unsigned 7",
"""
int main(){
unsigned b = 1;
assert(1 == b);
return 0;
}
"""
)
test("type_unsigned 8",
"""
int main(){
unsigned b = 1;
assert(b == 1);
return 0;
}
"""
)
test("type_compatibility 1",
"""
int main(){
int a = 4;
char b = 6;
b = a;
assert(b==4);
return 0;
}
"""
)
test("type_compatibility 2",
"""
int main(){
int a[2];
char b[2];
b = a;
return 0;
}
"""
)
test("type_compatibility 3",
"""
int main(){
int a[2];
char b[2];
a = b;
return 0;
}
"""
)
test("type_compatibility 4",
"""
int main(){
long a[2];
char b[2];
a = b;
return 0;
}
"""
)
test("type_compatibility 5",
"""
int main(){
long a[2];
char b[2];
a = b;
return 0;
}
"""
)
test("string 1 a",
"""
int main(){
int c[4] = "aA1";
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
return 0;
}
""")
test("string 1 b",
"""
int main(){
int c[4] = "aA1";
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
return 0;
}
""", True)
test("string 2a",
"""
int main(){
int c[] = "aA1";
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
return 0;
}
""")
test("string 2b",
"""
int main(){
int c[] = "aA1";
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
return 0;
}
""", True)
test_fails("string 3",
"""
int main(){
int c[];
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
return 0;
}
""")
test("string 4a",
"""
int test(char c[]){
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
c[0] = 'b';
c[1] = 'B';
c[2] = '2';
c[3] = 0;
return 0;
}
int main(){
char c[] = "aA1";
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
test(c);
assert(c[0] == 'b');
assert(c[1] == 'B');
assert(c[2] == '2');
assert(c[3] == 0);
return 0;
}
""")
test("string 4b",
"""
int test(char c[]){
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
c[0] = 'b';
c[1] = 'B';
c[2] = '2';
c[3] = 0;
return 0;
}
int main(){
char c[] = "aA1";
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
test(c);
assert(c[0] == 'b');
assert(c[1] == 'B');
assert(c[2] == '2');
assert(c[3] == 0);
return 0;
}
""", True)
test_fails("string 5",
"""
int main(){
int c[] "\\n";
assert(c[0] == 10);
assert(c[1] == 0);
return 0;
}
""")
test("string 6a",
"""
int test(char c[]){
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
return 0;
}
int main(){
test("aA1");
return 0;
}
""")
test("string 6b",
"""
int test(char c[]){
assert(c[0] == 97);
assert(c[1] == 65);
assert(c[2] == 49);
assert(c[3] == 0);
return 0;
}
int main(){
test("aA1");
return 0;
}
""", True)
test("string 7a",
"""
int main(){
char c[] = "a\\n";
assert(c[0] == 97);
assert(c[1] == 10);
assert(c[2] == 0);
return 0;
}
""")
test("string 7b",
"""
int main(){
char c[] = "a\\n";
assert(c[0] == 97);
assert(c[1] == 10);
assert(c[2] == 0);
return 0;
}
""", True)
test("char 1",
"""
int main(){
int c = 'a';
assert(c == 97);
return 0;
}
""")
test("char 2",
"""
int main(){
int c = 'A';
assert(c == 65);
return 0;
}
""")
test("char 3",
"""
int main(){
int c = '1';
assert(c == 49);
return 0;
}
""")
test("char 4",
"""
int main(){
int c = '\\n';
assert(c == 10);
return 0;
}
""")
test("unsigned long modulo 1",
"""
unsigned long int main(){
unsigned long int a = 3;
unsigned long int b = 4;
assert(a%b == 3);
return 0;
}
""")
test("unsigned long modulo 3",
"""
unsigned long int main(){
unsigned long int a = 7;
unsigned long int b = 8;
assert(a%b == 7);
return 0;
}
""")
test("unsigned long modulo 4",
"""
unsigned long int main(){
unsigned long int a = 15;
unsigned long int b = 8;
assert(a%b == 7);
return 0;
}
""")
test("unsigned long modulo 9",
"""
unsigned long int main(){
unsigned long int a = 32766;
unsigned long int b = 0;
assert(a%b == 32766);
return 0;
}
""")
test("unsigned long division 1",
"""
unsigned long int main(){
unsigned long int a = 15;
unsigned long int b = 3;
assert(a/b == 5);
return 0;
}
""")
test("unsigned long division 2",
"""
unsigned long int main(){
unsigned long int a = 12;
unsigned long int b = 4;
assert(a/b == 3);
return 0;
}
""")
test("unsigned long division 3",
"""
unsigned long int main(){
unsigned long int a = 1;
unsigned long int b = 1;
assert(a/b == 1);
return 0;
}
""")
test("unsigned long division 6",
"""
unsigned long int main(){
unsigned long int a = 0;
unsigned long int b = 1;
assert(a/b == 0);
return 0;
}
""")
test("unsigned long division 7",
"""
unsigned long int main(){
unsigned long int a = 5;
unsigned long int b = 2;
assert(a/b == 2);
return 0;
}
""")
test("unsigned long division 9",
"""
unsigned long int main(){
unsigned long int a = 0;
unsigned long int b = 32767;
assert(a/b == 0);
return 0;
}
""")
test("unsigned long division 10",
"""
unsigned long int main(){
unsigned long int a = 32767;
unsigned long int b = 1;
assert(a/b == 32767);
return 0;
}
""")
test("unsigned long division 11",
"""
unsigned long int main(){
unsigned long int a = 32767;
unsigned long int b = 2;
assert(a/b == 16383);
return 0;
}
""")
test("unsigned long division 12",
"""
unsigned long int main(){
unsigned long int a = 32767;
unsigned long int b = 32767;
assert(a/b == 1);
return 0;
}
""")
test("unsigned modulo 1",
"""
unsigned int main(){
unsigned int a = 3;
unsigned int b = 4;
assert(a%b == 3);
return 0;
}
""")
test("unsigned modulo 3",
"""
unsigned int main(){
unsigned int a = 7;
unsigned int b = 8;
assert(a%b == 7);
return 0;
}
""")
test("unsigned modulo 4",
"""
unsigned int main(){
unsigned int a = 15;
unsigned int b = 8;
assert(a%b == 7);
return 0;
}
""")
test("unsigned modulo 9",
"""
unsigned int main(){
unsigned int a = 32766;
unsigned int b = 0;
assert(a%b == 32766);
return 0;
}
""")
test("unsigned division 1",
"""
unsigned int main(){
unsigned int a = 15;
unsigned int b = 3;
assert(a/b == 5);
return 0;
}
""")
test("unsigned division 2",
"""
unsigned int main(){
unsigned int a = 12;
unsigned int b = 4;
assert(a/b == 3);
return 0;
}
""")
test("unsigned division 3",
"""
unsigned int main(){
unsigned int a = 1;
unsigned int b = 1;
assert(a/b == 1);
return 0;
}
""")
test("unsigned division 6",
"""
unsigned int main(){
unsigned int a = 0;
unsigned int b = 1;
assert(a/b == 0);
return 0;
}
""")
test("unsigned division 7",
"""
unsigned int main(){
unsigned int a = 5;
unsigned int b = 2;
assert(a/b == 2);
return 0;
}
""")
test("unsigned division 9",
"""
unsigned int main(){
unsigned int a = 0;
unsigned int b = 32767;
assert(a/b == 0);
return 0;
}
""")
test("unsigned division 10",
"""
unsigned int main(){
unsigned int a = 32767;
unsigned int b = 1;
assert(a/b == 32767);
return 0;
}
""")
test("unsigned division 11",
"""
unsigned int main(){
unsigned int a = 32767;
unsigned int b = 2;
assert(a/b == 16383);
return 0;
}
""")
test("unsigned division 12",
"""
unsigned int main(){
unsigned int a = 32767;
unsigned int b = 32767;
assert(a/b == 1);
return 0;
}
""")
test("long modulo 1",
"""
long int main(){
long int a = 3;
long int b = 4;
assert(a%b == 3);
return 0;
}
""")
test("long modulo 2",
"""
long int main(){
long int a = -3;
long int b = 4;
assert(a%b == -3);
return 0;
}
""")
test("long modulo 3",
"""
long int main(){
long int a = 7;
long int b = 8;
assert(a%b == 7);
return 0;
}
""")
test("long modulo 4",
"""
long int main(){
long int a = 15;
long int b = 8;
assert(a%b == 7);
return 0;
}
""")
test("long modulo 5",
"""
long int main(){
long int a = -7;
long int b = 8;
assert(a%b == -7);
return 0;
}
""")
test("long modulo 6",
"""
long int main(){
long int a = -15;
long int b = 8;
assert(a%b == -7);
return 0;
}
""")
test("long modulo 7",
"""
long int main(){
long int a = 7;
long int b = -8;
assert(a%b == 7);
return 0;
}
""")
test("long modulo 8",
"""
long int main(){
long int a = 15;
long int b = -8;
assert(a%b == 7);
return 0;
}
""")
test("long modulo 9",
"""
long int main(){
long int a = 32766;
long int b = 0;
assert(a%b == 32766);
return 0;
}
""")
test("long division 1",
"""
long int main(){
long int a = 15;
long int b = 3;
assert(a/b == 5);
return 0;
}
""")
test("long division 2",
"""
long int main(){
long int a = 12;
long int b = 4;
assert(a/b == 3);
return 0;
}
""")
test("long division 3",
"""
long int main(){
long int a = 1;
long int b = 1;
assert(a/b == 1);
return 0;
}
""")
test("long division 4",
"""
long int main(){
long int a = 1;
long int b = -1;
assert(a/b == -1);
return 0;
}
""")
test("long division 5",
"""
long int main(){
long int a = -1;
long int b = 1;
assert(a/b == -1);
return 0;
}
""")
test("long division 6",
"""
long int main(){
long int a = 0;
long int b = 1;
assert(a/b == 0);
return 0;
}
""")
test("long division 7",
"""
long int main(){
long int a = 5;
long int b = 2;
assert(a/b == 2);
return 0;
}
""")
test("long division 8",
"""
long int main(){
long int a = -5;
long int b = 2;
assert(a/b == -2);
return 0;
}
""")
test("long division 9",
"""
long int main(){
long int a = 0;
long int b = 32767;
assert(a/b == 0);
return 0;
}
""")
test("long division 10",
"""
long int main(){
long int a = 32767;
long int b = 1;
assert(a/b == 32767);
return 0;
}
""")
test("long division 11",
"""
long int main(){
long int a = 32767;
long int b = 2;
assert(a/b == 16383);
return 0;
}
""")
test("long division 12",
"""
long int main(){
long int a = 32767;
long int b = 32767;
assert(a/b == 1);
return 0;
}
""")
test("long division 13",
"""
long int main(){
long int a = -32767-1;
long int b = -32767-1;
assert(a/b == 1);
return 0;
}
""")
test("long division 14",
"""
long int main(){
long int a = -32767;
long int b = -1;
assert(a/b == 32767);
return 0;
}
""")
test("modulo 1",
"""
int main(){
int a = 3;
int b = 4;
assert(a%b == 3);
return 0;
}
""")
test("modulo 2",
"""
int main(){
int a = -3;
int b = 4;
assert(a%b == -3);
return 0;
}
""")
test("modulo 3",
"""
int main(){
int a = 7;
int b = 8;
assert(a%b == 7);
return 0;
}
""")
test("modulo 4",
"""
int main(){
int a = 15;
int b = 8;
assert(a%b == 7);
return 0;
}
""")
test("modulo 5",
"""
int main(){
int a = -7;
int b = 8;
assert(a%b == -7);
return 0;
}
""")
test("modulo 6",
"""
int main(){
int a = -15;
int b = 8;
assert(a%b == -7);
return 0;
}
""")
test("modulo 7",
"""
int main(){
int a = 7;
int b = -8;
assert(a%b == 7);
return 0;
}
""")
test("modulo 8",
"""
int main(){
int a = 15;
int b = -8;
assert(a%b == 7);
return 0;
}
""")
test("modulo 9",
"""
int main(){
int a = 32766;
int b = 0;
assert(a%b == 32766);
return 0;
}
""")
test("division 1",
"""
int main(){
int a = 15;
int b = 3;
assert(a/b == 5);
return 0;
}
""")
test("division 2",
"""
int main(){
int a = 12;
int b = 4;
assert(a/b == 3);
return 0;
}
""")
test("division 3",
"""
int main(){
int a = 1;
int b = 1;
assert(a/b == 1);
return 0;
}
""")
test("division 4",
"""
int main(){
int a = 1;
int b = -1;
assert(a/b == -1);
return 0;
}
""")
test("division 5",
"""
int main(){
int a = -1;
int b = 1;
assert(a/b == -1);
return 0;
}
""")
test("division 6",
"""
int main(){
int a = 0;
int b = 1;
assert(a/b == 0);
return 0;
}
""")
test("division 7",
"""
int main(){
int a = 5;
int b = 2;
assert(a/b == 2);
return 0;
}
""")
test("division 8",
"""
int main(){
int a = -5;
int b = 2;
assert(a/b == -2);
return 0;
}
""")
test("division 9",
"""
int main(){
int a = 0;
int b = 32767;
assert(a/b == 0);
return 0;
}
""")
test("division 10",
"""
int main(){
int a = 32767;
int b = 1;
assert(a/b == 32767);
return 0;
}
""")
test("division 11",
"""
int main(){
int a = 32767;
int b = 2;
assert(a/b == 16383);
return 0;
}
""")
test("division 12",
"""
int main(){
int a = 32767;
int b = 32767;
assert(a/b == 1);
return 0;
}
""")
test("division 13",
"""
int main(){
int a = -32767-1;
int b = -32767-1;
assert(a/b == 1);
return 0;
}
""")
test("division 14",
"""
int main(){
int a = -32767;
int b = -1;
assert(a/b == 32767);
return 0;
}
""")
test("struct with array 1",
"""
typedef struct {int a; int b; int c[2];} blah;
blah myblah;
void test(){
assert(myblah.a == 1);
assert(myblah.b == 2);
assert(myblah.c[0] == 3);
assert(myblah.c[1] == 4);
myblah.a = 5;
myblah.b = 6;
myblah.c[0] = 7;
myblah.c[1] = 8;
}
void main(){
myblah.a = 1;
myblah.b = 2;
myblah.c[0] = 3;
myblah.c[1] = 4;
test();
assert(myblah.a == 5);
assert(myblah.b == 6);
assert(myblah.c[0] == 7);
assert(myblah.c[1] == 8);
}
"""
)
test("global variables 1",
"""
int a;
void test(){
assert(a==12);
a = 24;
}
void main(){
a = 12;
test();
assert(a==24);
}
"""
)
test("global variables 2",
"""
int a[10];
void test(){
assert(a[0] == 0);
assert(a[1] == 1);
assert(a[2] == 2);
assert(a[3] == 3);
a[0] = 4;
a[1] = 5;
a[2] = 6;
a[3] = 7;
}
void main(){
a[0] = 0;
a[1] = 1;
a[2] = 2;
a[3] = 3;
test();
assert(a[0] == 4);
assert(a[1] == 5);
assert(a[2] == 6);
assert(a[3] == 7);
}
"""
)
test("global variables 3",
"""
int a=0, b=1, c=2;
void test(){
assert(a == 3);
assert(b == 4);
assert(c == 5);
a=6;
b=7;
c=8;
}
void main(){
assert(a == 0);
assert(b == 1);
assert(c == 2);
a=3;
b=4;
c=5;
test();
assert(a == 6);
assert(b == 7);
assert(c == 8);
}
"""
)
test("global variables 4",
"""
typedef struct {int a; int b; int c;} blah;
blah myblah;
void test(){
assert(myblah.a == 1);
assert(myblah.b == 2);
assert(myblah.c == 3);
myblah.a = 4;
myblah.b = 5;
myblah.c = 6;
}
void main(){
myblah.a = 1;
myblah.b = 2;
myblah.c = 3;
test();
assert(myblah.a == 4);
assert(myblah.b == 5);
assert(myblah.c == 6);
}
"""
)
test("void functions 1",
"""
void main(){
int a;
int b;
b = a;
}
"""
)
test("void functions 2",
"""
void test(){
return;
}
void main(){
test();
}
"""
)
test_fails("void functions 3",
"""
void test(){
return;
}
void main(){
int a;
a = test();
}
"""
)
test_fails("void functions 4",
"""
void test(){
return 10;
}
void main(){
int a;
a = test();
}
"""
)
test_fails("type_checking 1",
"""
int main(){
int a[4];
int b;
b = a;
return 0;
}
"""
)
test_fails("type_checking 2",
"""
int main(){
int a[4];
int b[4];
b[a]=12;
return 0;
}
"""
)
test_fails("type_checking 3",
"""
int main(){
int a[4];
int b;
if(a) b=12;
return 0;
}
"""
)
test_fails("type_checking 4",
"""
int main(){
int a[4];
int b;
while(a) b=12;
return 0;
}
"""
)
test_fails("type_checking 5",
"""
int main(){
int a[4];
int b;
for(;a;) b=12;
return 0;
}
"""
)
test_fails("type_checking 6",
"""
int main(){
int a[4];
int b;
switch(a){}
return 0;
}
"""
)
test_fails("type_checking 7",
"""
int main(){
int a[4];
int b;
switch(b){case a:b=12;}
return 0;
}
"""
)
test_fails("type_checking 8",
"""
int test(int a[]){
return 0;
}
int main(){
int b;
test(b);
return 0;
}
"""
)
test_fails("type_checking 9",
"""
int test(int a){
return 0;
}
int main(){
int b[4];
test(b);
return 0;
}
"""
)
test("array passing 1",
"""
int test(int a[]){
assert(a[0] == 0);
assert(a[1] == 1);
assert(a[2] == 2);
assert(a[3] == 3);
a[0] = 4;
a[1] = 5;
a[2] = 6;
a[3] = 7;
return 0;
}
int main(){
int a[4];
a[0] = 0;
a[1] = 1;
a[2] = 2;
a[3] = 3;
test(a);
assert(a[0] == 4);
assert(a[1] == 5);
assert(a[2] == 6);
assert(a[3] == 7);
return 0;
}
"""
)
test("array passing 2",
"""
int test(int a[]){
assert(a[0] == 0);
assert(a[1] == 1);
assert(a[2] == 2);
assert(a[3] == 3);
a[0] = 4;
a[1] = 5;
a[2] = 6;
a[3] = 7;
return 0;
}
int main(){
int a[4];
int b[4];
a[0] = 0;
a[1] = 1;
a[2] = 2;
a[3] = 3;
b = a;
test(b);
assert(a[0] == 4);
assert(a[1] == 5);
assert(a[2] == 6);
assert(a[3] == 7);
return 0;
}"""
)
test("struct 1",
"""
struct blah {int a; int b; int c;};
int main(){
struct blah myblah;
myblah.a = 1;
myblah.b = 2;
myblah.c = 3;
assert(myblah.a == 1);
assert(myblah.b == 2);
assert(myblah.c == 3);
return 0;
}
"""
)
test("struct 2",
"""
struct as {int a; int b; int c;};
int main(){
struct as asa;
struct as asb;
asa.a = 1;
asb.a = 3;
asa.b = 2;
asb.b = 2;
asa.c = 3;
asb.c = 1;
assert(asa.a == 1);
assert(asb.a == 3);
assert(asa.b == 2);
assert(asb.b == 2);
assert(asa.c == 3);
assert(asb.c == 1);
return 0;
}
"""
)
test("struct 3",
"""
typedef struct {int a; int b; int c;} blah;
int main(){
blah myblah;
myblah.a = 1;
myblah.b = 2;
myblah.c = 3;
assert(myblah.a == 1);
assert(myblah.b == 2);
assert(myblah.c == 3);
return 0;
}
"""
)
test("struct 4",
"""
typedef struct{int a; int b; int c;} mytype;
typedef struct{mytype a;} othertype;
int main(){
othertype a;
othertype b;
a.a.a = 1;
b.a.a = 2;
a.a.b = 3;
b.a.b = 4;
a.a.c = 5;
b.a.c = 6;
assert(a.a.a == 1);
assert(b.a.a == 2);
assert(a.a.b == 3);
assert(b.a.b == 4);
assert(a.a.c == 5);
assert(b.a.c == 6);
return 0;
}
"""
)
test("include 1",
"""#include "test_include.c"
int main(){
assert(include_function()==12);
return 0;
}
"""
)
test("switch 1",
"""int main(){
switch(0){
case 0: return 3;
case 1: return 2;
case 2: return 1;
default: return 0;
}
}
"""
)
test("switch 2",
"""int main(){
switch(2){
case 0: return 3;
case 1: return 2;
case 2: return 1;
default: return 0;
}
}
"""
)
test("switch 3",
"""int main(){
switch(5){
case 0: return 3;
case 1: return 2;
case 2: return 1;
default: return 0;
}
}
"""
)
test("switch 4",
"""int main(){
int a = 0;
switch(0){
case 0: a = 1;
case 1: a = 2;
case 2: a = 3;
default: a = 4;
}
return a;
}
"""
)
test("switch 5",
"""int main(){
int a = 0;
switch(1){
case 0: a = 1;
case 1: a = 2;
case 2: a = 3;
default: a = 4;
}
return a;
}
"""
)
test("switch 6",
"""int main(){
int a = 1;
switch(10){
case 0: a = 1;
case 1: a = 2;
case 2: a = 3;
default: a = 4;
}
return a;
}
"""
)
test("switch 7",
"""int main(){
int a = 1;
switch(0){
case 0: a = 1; break;
case 1: a = 2; break;
case 2: a = 3; break;
default: a = 4; break;
}
return a;
}
"""
)
test("switch 8",
"""int main(){
int a = 1;
switch(2){
case 0: a = 1; break;
case 1: a = 2; break;
case 2: a = 3; break;
default: a = 4; break;
}
return a;
}
"""
)
test("switch 9",
"""int main(){
int a = 1;
switch(9){
case 0: a = 1; break;
case 1: a = 2; break;
case 2: a = 3; break;
default: a = 4; break;
}
return a;
}
"""
)
test("break 0",
"""
int main(){
int a;
while(1){
break;
assert(0);
}
return 0;
}
""")
test("break 1",
"""
int main(){
int a;
for(a=0; a<20; a++){
if(a == 10){
break;
}
}
assert(a == 10);
return 0;
}
""")
test("continue 0",
"""
int main(){
int a;
for(a=1; a<=10; a++){
if(a <= 5){
continue;
}
assert(a > 5);
}
return 0;
}
""")
test("ternary 0",
"""
int main(){
int a;
int b=2;
int c=3;
assert((1?2:3) == 2);
assert((0?2:3) == 3);
a = 1;
assert((a?b:c) == 2);
a = 0;
assert((a?b:c) == 3);
assert((1?b:c) == 2);
assert((0?b:c) == 3);
return 0;
}
""")
test("inplace 0",
"""
int main(){
int a = 1;
a += 1;
assert(a == 2);
a -= 1;
assert(a == 1);
a *= 2;
assert(a == 2);
a /= 2;
assert(a == 1);
a |= 2;
assert(a == 3);
a &= 2;
assert(a == 2);
a <<= 1;
assert(a == 4);
a >>= 1;
assert(a == 2);
return 0;
}
""")
test("inplace 1",
"""
int main(){
int a[100];
a[0] = 1;
a[20] = 1;
a[20] += 1;
assert(a[20] == 2);
a[20] -= 1;
assert(a[20] == 1);
a[20] *= 2;
assert(a[20] == 2);
a[20] /= 2;
assert(a[20] == 1);
a[20] |= 2;
assert(a[20] == 3);
a[20] &= 2;
assert(a[20] == 2);
a[20] <<= 1;
assert(a[20] == 4);
a[20] >>= 1;
assert(a[20] == 2);
assert(a[0] == 1);
return 0;
}
""")
test("increment",
"""
int main(){
int a = 1;
a++;
assert(a == 2);
a--;
assert(a == 1);
return 0;
}
""")
test("assert 0",
"""int main(){
assert(1);
return 0;
}
""")
test_fails("assert 1",
"""int main(){
assert(0);
return 0;
}
""")
test("assign",
"""int main(){
int a;
int b;
int c;
a = 10;
b = 20;
c = a + b;
assert(a == 10);
assert(b == 20);
assert(c == 30);
return 0;
}
""")
test("while",
"""int main(){
int a = 10;
int b = 0;
while(a){
a = a - 1;
b = b + 1;
}
assert(b == 10);
return 0;
}
""")
test("while 1",
"""int main(){
int a = 0;
while(1){
a = a + 1;
if(a == 10){
return 0;
}
}
}
""")
test("while 2",
"""int main(){
while(0){
assert(0);
}
return 0;
}
""")
test("if",
"""int main(){
int a = 0;
int b = 0;
if(a){
b = 10;
assert(0);
} else {
b = 20;
}
assert(b == 20);
return 0;
}
""")
test("if 1",
"""int main(){
int a = 1;
int b = 0;
if(a){
b = 10;
} else {
b = 20;
assert(0);
}
assert(b == 10);
return 0;
}
""")
test("if 2",
"""int main(){
int b = 0;
if(0){
b = 10;
assert(0);
} else {
b = 20;
}
assert(b == 20);
return 0;
}
""")
test("if 3",
"""int main(){
int b = 0;
if(1){
b = 10;
} else {
b = 20;
assert(0);
}
assert(b == 10);
return 0;
}
""")
test("if 4",
"""int main(){
int b = 0;
if(0){
b = 10;
assert(0);
}
assert(b == 0);
return 0;
}
""")
test("for 0",
"""int main(){
int a = 0;
int b;
int c = 1;
for(a=0; a<10; a++){
b = b + 1;
c = c + 1;
}
assert(b == 10);
assert(c == 11);
return 0;
}
""")
test("for 1",
"""int main(){
int a = 0;
int b;
int c = 1;
for(; a<10; a++){
b = b + 1;
c = c + 1;
}
assert(b == 10);
assert(c == 11);
return 0;
}
""")
test("for 2",
"""int main(){
int a = 0;
int b;
int c = 1;
for(;a<10;){
b = b + 1;
c = c + 1;
a++;
}
assert(b == 10);
assert(c == 11);
return 0;
}
""")
test("for 3",
"""int main(){
int a = 0;
int b;
int c = 1;
for(;;){
if(a>=10) break;
b = b + 1;
c = c + 1;
a++;
}
assert(b == 10);
assert(c == 11);
return 0;
}
""")
test("number 0",
"""int main(){
return 1;
}
""")
test("report 0",
"""int main(){
int a = 0;
int b = 1;
int c = 2;
report(a);
report(b);
report(c);
return 0;
}
""")
test("declare 0",
"""int main(){
int a = 10;
int b = 20, c = 30;
int d[100], e[200];
assert(a==10);
assert(b==20);
assert(c==30);
return 0;
}
""")
test("wait_clocks 0",
"""int main(){
int a = 10;
wait_clocks(a);
wait_clocks(10);
return 0;
}
""")
test("function",
"""
int func(int a){
return a + 10;
}
int main(){
int a = func(10);
assert(a == 20);
return 0;
}
""")
test("function 1",
"""
int func(int a){
assert(a == 20);
return 0;
}
int main(){
func(20);
return 0;
}
""")
test("function 2",
"""
int func(int a, int b, int c){
return a;
}
int main(){
assert(func(1, 2, 3) == 1);
return 0;
}
""")
test("function 3",
"""
int func(int a, int b, int c){
return b;
}
int main(){
assert(func(1, 2, 3) == 2);
return 0;
}
""")
test("function 4",
"""
int func(int a, int b, int c){
return c;
}
int main(){
assert(func(1, 2, 3) == 3);
return 0;
}
""")
test("function 5",
"""
int another(int a){
return a + 1;
}
int func(int a){
return another(a) + 1;
}
int main(){
assert(func(0) == 2);
return 0;
}
""")
test_fails("function 6",
"""
int func(int a, int b){
return b;
}
int main(){
assert(func(1, 2, 3) == 3);
return 0;
}
""")
test("expression 1",
"""
int main(){
int a = 1;
int b = 2;
int c = 3;
assert(a + b + c == 6);
return 0;
}
""")
test("expression 2",
"""
int main(){
int a = 1;
int b = 2;
int c = 3;
assert(a - b - c == -4);
return 0;
}
""")
test("expression 3",
"""
int main(){
int a = 1;
int b = 2;
int c = 3;
assert(a - (b - c) == 2);
return 0;
}
""")
test("expression 4",
"""
int main(){
int a = 1;
int b = 2;
int c = 3;
assert(a * b * c == 6);
return 0;
}
""")
test("expression 5",
"""
int main(){
int a = 1;
int b = 2;
int c = 3;
assert(a/b/c == 0);
return 0;
}
""")
test("expression 6",
"""
int main(){
int a = 1;
int b = 2;
int c = 3;
assert(a%b%c == 1);
return 0;
}
""")
test("expression 7",
"""
int main(){
int a = 1;
int b = 2;
int c = 3;
assert(-a - (b - c) == 0);
return 0;
}
""")
test("expression 8",
"""
int fail(){
assert(0);
return 0;
}
int main(){
int a = 0 && fail();
return 0;
}
""")
test("expression 9",
"""
int fail(){
assert(0);
return 0;
}
int main(){
int a = 1 || fail();
return 0;
}
""")
test("expression 10",
"""
int main(){
int a = 1;
assert(a << 2 == 4);
return 0;
}
""")
test("expression 11",
"""
int main(){
int a = 1;
int b = 2;
assert(a << b == 4);
return 0;
}
""")
test("expression 12",
"""
int main(){
int a = 4;
assert(a >> 2 == 1);
return 0;
}
""")
test("expression 13",
"""
int main(){
int a = 4;
int b = 2;
assert(a >> b == 1);
return 0;
}
""")
test("expression 14",
"""
int main(){
int a = -1;
assert(~a == 0);
return 0;
}
""")
test("expression 15",
"""
int main(){
int a = 1;
assert(!a == 0);
int a = 0;
assert(!a == 1);
return 0;
}
""")
test("expression 16",
"""
int main(){
int a = 0xA;
int b = 0x5;
assert((a | b) == 0xF);
assert((a ^ b) == 0xf);
assert((a & b) == 0);
return 0;
}
""")
test("expression 17",
"""
int fail(){
assert(0);
return 0;
}
int main(){
int b = 0;
int a = b && fail();
return 0;
}
""")
test("expression 18",
"""
int fail(){
assert(0);
return 0;
}
int main(){
assert(~1);
return 0;
}
""")
test("expression 19",
"""
int main(){
assert(-1 < 1);
assert(-1 < 0);
assert(0 <= 0);
assert(0 >= 0);
assert(1 >= 0);
assert(1 >= -1);
assert(1 > -1);
assert(1 > 0);
assert(12 != 13);
assert(100 == 100);
return 0;
}
""")
test("expression 20",
"""
int main(){
int a = -1;
assert(a >> 2 == -1);
return 0;
}
""")
test("comment 0",
"""
int main(){
//assert(0);
//assert(0);
//assert(0);
return 0;
}
""")
test("comment 1",
"""
int main(){
/*assert(0);
assert(0);
assert(0);*/
return 0;
}
""")
test("array 0",
"""
int main(){
int a [1024];
int b [1024];
a[0] = 1;
a[1] = 2;
a[3] = 3;
assert(a[0] == 1);
assert(a[1] == 2);
assert(a[3] == 3);
return 0;
}
""")
test("array 1",
"""
int main(){
int a [1024];
int b [1024];
a[0] = 10;
b[0] = 20;
a[1] = 30;
b[1] = 40;
a[3] = 50;
b[3] = 60;
assert(a[0] == 10);
assert(b[0] == 20);
assert(a[1] == 30);
assert(b[1] == 40);
assert(a[3] == 50);
assert(b[3] == 60);
return 0;
}
""")
test_fails("error 0",
"""
int main(){
int a;
a = c;
return 0;
}
""")
test_fails("error 1",
"""
int main(){
int a;
}
""")
test_fails("error 2",
"""
int main(){
int a blah;
}
""")
test_fails("error 3",
"""
int main(){
int a;
b = a;
}
""")
test_fails("error 4",
"""
int main(){
int a;
a = c();
}
""")
test_fails("error 5",
"""
int main(){
int a;
a==;
}
""")
test_fails("error 6",
"""
int main(){
int a;
a=00x;
}
""")
test_fails("error 7",
"""
int main(){
switch(1){
case 0:
default:
default:
}
return 0;
}
""")
test_fails("error 8",
"""
int main(){
default:
return 0;
}
""")
test_fails("error 9",
"""
int main(){
case 1:
return 0;
}
""")
test_fails("error 10",
"""
int main(){
int a = 12;
switch(a){
case a + 1:
a++;
}
return 0;
}
""")
test_fails("error 11",
"""
int myfunction(){
return 0;
}
int main(){
int a = 12;
myfunction()=10;
return 0;
}
""")
test("input 1",
"""
int main(){
int b;
b = input_a();
return 0;
}
""")
test("output 1",
"""
int main(){
output_a(12);
return 0;
}
""")
test("input output 1",
"""
int main(){
if (input_select()){
output_z(input_a());
} else {
output_z(input_b());
}
return 0;
}
""")
test("input output 2",
"""
int arbiter(){
while(1){
if(ready_a()) output_z(input_a());
if(ready_b()) output_z(input_b());
}
return 0;
}
""")
test("main not main",
"""
int main(){
assert(0);
return 0;
}
//last function is always main
int real_main(){
return 0;
}
""")
os.system("python-coverage run -p c2verilog.py")
os.system("python-coverage combine")
os.system("python-coverage report")
os.system("python-coverage annotate c2verilog.py")
os.system("python-coverage annotate compiler/parser.py")
Go to most recent revision | Compare with Previous | Blame | View Log