[svn r5] Initial commit. Most things are very rough.

This commit is contained in:
Tomas Lindquist Olsen
2007-09-01 21:43:27 +02:00
parent 1c23dd2cdc
commit 34699bbb07
227 changed files with 84269 additions and 0 deletions

21
test/a.d Normal file
View File

@@ -0,0 +1,21 @@
module a;
int i = 42;
void main()
{
int j;
j = i;
int* p;
p = &j;
int k = *p;
assert(k == 42);
byte b = -1;
int i = b;
assert(i == -1);
int* p2 = p;
printf("Done\n");
}

21
test/alignment.d Normal file
View File

@@ -0,0 +1,21 @@
module alignment;
align(1) struct S
{
ubyte b;
int i;
ubyte[2] b2;
long l;
real r;
}
void main()
{
byte b;
short s;
int i;
long l;
float f;
double d;
real r;
}

5
test/arrayinit.d Normal file
View File

@@ -0,0 +1,5 @@
module arrayinit;
float[4] ftable = [1,2,3,4];
int[8] itable = [3:42,6:123];
private uint[7] crc32_table = [0x00000000,0x77073096,0xee0e612c,0x990951ba,0x076dc419,0x706af48f,0xe963a535];

39
test/arrays.d Normal file
View File

@@ -0,0 +1,39 @@
void integer()
{
auto arr = new int[16];
arr[1] = 42;
arr[6] = 555;
print_int(arr);
delete arr;
}
void floating()
{
auto arr = new float[6];
arr[1] = 3.14159265;
arr[3] = 1.61803399;
print_float(arr);
delete arr;
}
void print_int(int[] arr)
{
printf("arr[%lu] = [", arr.length);
for (auto i=0; i<arr.length; i++)
printf("%d,", arr[i]);
printf("\b]\n");
}
void print_float(float[] arr)
{
printf("arr[%lu] = [", arr.length);
for (auto i=0; i<arr.length; i++)
printf("%f,", arr[i]);
printf("\b]\n");
}
void main()
{
integer();
floating();
}

19
test/arrays2.d Normal file
View File

@@ -0,0 +1,19 @@
char[] get()
{
return "hello";
}
void param(char[] s)
{
}
void refparam(ref char[] s)
{
}
void main()
{
char[] dstr = get();
param(dstr);
refparam(dstr);
}

23
test/assign.d Normal file
View File

@@ -0,0 +1,23 @@
module assign;
// this is taken from std.intrinsic from gdc
int mybtc(uint *p, uint bitnum)
{
uint * q = p + (bitnum / (uint.sizeof*8));
uint mask = 1 << (bitnum & ((uint.sizeof*8) - 1));
int result = *q & mask;
*q ^= mask;
return result ? -1 : 0;
}
void main()
{
uint i = 0xFFFF_FFFF;
int r = mybtc(&i, 31);
assert(r);
assert(i == 0x7FFF_FFFF);
r = mybtc(&i, 31);
assert(!r);
assert(i == 0xFFFF_FFFF);
}

23
test/b.d Normal file
View File

@@ -0,0 +1,23 @@
module b;
struct S
{
int i;
float[4] f;
}
void main()
{
S s;
int i = s.i;
int* p = &s.i;
*p = 42;
printf("%d == %d\n", *p, s.i);
float* f = &s.f[0];
printf("%f == %f\n", *f, s.f[0]);
*f = 3.1415;
printf("%f == %f\n", *f, s.f[0]);
s.f[0] = 123.456;
printf("%f == %f\n", *f, s.f[0]);
}

75
test/bitops.d Normal file
View File

@@ -0,0 +1,75 @@
void main()
{
printf("Bitwise operations test\n");
{ ushort a = 0xFFF0;
ushort b = 0x0FFF;
assert((a&b) == 0x0FF0);
assert((a|b) == 0xFFFF);
assert((a^b) == 0xF00F);
}
{ ubyte a = 0xFF;
ulong b = 0xFFFF_FFFF_FFFF_FFF0;
assert((a&b) == 0xF0);
}
{ ushort s = 0xFF;
assert((s<<1) == s*2);
assert((s>>>1) == s/2);
}
{ int s = -10;
assert((s>>1) == -5);
assert((s>>>1) != -5);
}
{ ushort a = 0xFFF0;
ushort b = 0x0FFF;
auto t = a;
t &= b;
assert(t == 0x0FF0);
t = a;
t |= b;
assert(t == 0xFFFF);
t = a;
t ^= b;
assert(t == 0xF00F);
}
{ ubyte a = 0xFF;
ulong b = 0xFFFF_FFFF_FFFF_FFF0;
a &= b;
assert(a == 0xF0);
}
{ ushort s = 0xFF;
auto t = s;
t <<= 1;
assert(t == (s*2));
t = s;
t >>>= 1;
assert(t == s/2);
}
{ int s = -10;
auto t = s;
t >>= 1;
assert(t == -5);
t = s;
t >>>= 1;
assert(t != -5);
}
{ struct S
{
uint i;
ulong l;
};
S s = S(1,4);
auto a = s.i | s.l;
assert(a == 5);
s.i = 0xFFFF_00FF;
s.l = 0xFFFF_FFFF_0000_FF00;
s.l ^= s.i;
assert(s.l == ulong.max);
s.i = 0x__FFFF_FF00;
s.l = 0xFF00FF_FF00;
s.i &= s.l;
assert(s.i == 0x00FF_FF00);
}
printf(" SUCCESS\n");
}

10
test/c.d Normal file
View File

@@ -0,0 +1,10 @@
module c;
void main()
{
ushort a = 0xFFF0;
ushort b = 0x0FFF;
auto t = a & b;
a &= b;
assert(t == a);
}

17
test/classes.d Normal file
View File

@@ -0,0 +1,17 @@
class C
{
int i;
void p()
{
printf("%d\n", i);
}
}
void main()
{
printf("should print 4\n");
C c = new C;
c.i = 4;
c.p();
//delete c;
}

23
test/classes2.d Normal file
View File

@@ -0,0 +1,23 @@
class A
{
int i;
void f()
{
printf("A.f\n");
}
}
class B : A
{
long l;
void f()
{
printf("B.f\n");
}
}
void main()
{
A a = new B;
a.f();
}

33
test/classes3.d Normal file
View File

@@ -0,0 +1,33 @@
class C
{
int c;
long f(long l)
{
return l;
}
}
class D : C
{
int d;
override long f(long l)
{
return l*2;
}
}
void main()
{
scope c = new C;
assert(c.f(25L) == 25);
scope d = new D;
assert(d.f(25L) == 50);
C cd = d;
assert(cd.f(25L) == 50);
assert(func(d,25L) == 50);
}
long func(C c, long l)
{
return c.f(l);
}

36
test/classes4.d Normal file
View File

@@ -0,0 +1,36 @@
class A
{
int i = 42;
double df = 3.1415;
this()
{
}
char[] toString()
{
return "A:Object";
}
}
class B : A
{
ubyte b;
char[] toString()
{
return "B:A";
}
}
void main()
{
scope a = new A;
char[] as = a.toString;
{printf("a.toString = '%.*s'\n", as.length, as.ptr);}
Object o = a;
char[] os = o.toString;
{printf("o.toString = '%.*s'\n", os.length, os.ptr);}
scope b = new B;
char[] bs = b.toString;
{printf("b.toString = '%.*s'\n", bs.length, bs.ptr);}
}

11
test/comma.d Normal file
View File

@@ -0,0 +1,11 @@
module comma;
void main()
{
int i=0,j=0;
for (; i<10; i++,j++)
{
}
assert(i == 10);
assert(j == 10);
}

43
test/cond.d Normal file
View File

@@ -0,0 +1,43 @@
version=AndAnd;
version=OrOr;
version(AndAnd)
void andand1()
{
int a,b;
a = 4;
b = 5;
assert(a == 4);
assert(b == 5);
assert(a+b == 9);
assert(a == 4 && b == 5);
assert(a != 3 && b == 5);
assert(a > 2);
assert(a < 54);
assert(a < b);
assert(a > b-2);
int apb = a+b;
int amb = a*b;
assert(apb < amb && apb > a);
}
version(OrOr)
void oror1()
{
int a,b;
a = 10;
b = 1000;
assert(a);
assert(b);
assert(a || b);
assert(a > b || a < b);
}
void main()
{
printf("Conditionals test\n");
version(AndAnd) andand1();
version(OrOr) oror1();
printf(" SUCCESS\n");
}

14
test/condexp.d Normal file
View File

@@ -0,0 +1,14 @@
module condexp;
int f()
{
return 42;
}
void main()
{
int i = f() < 25 ? -1 : 1;
/*int j = f() > 25 ? 1 : -1;
assert(i);
assert(!j);*/
}

16
test/cyclic.d Normal file
View File

@@ -0,0 +1,16 @@
void main()
{
S t;
}
struct T
{
int i;
S* s;
}
struct S
{
long i;
T* t;
}

48
test/d.d Normal file
View File

@@ -0,0 +1,48 @@
module d;
/*
void main()
{
int delegate() dg;
int i = dg();
struct S
{
int i;
long l;
float f;
int func()
{
return 42;
}
}
S s;
auto dg2 = &s.func;
i = dg2();
i = f(dg2, 1);
}
int f(int delegate() dg, int i)
{
return dg() + i;
}
*/
struct S
{
int i;
float f;
int square()
{
return i*i;
}
}
S s;
void main()
{
auto dg = &s.square;
}

84
test/dgs.d Normal file
View File

@@ -0,0 +1,84 @@
struct S
{
int i;
int square()
{
return i*i;
}
int plus(int a)
{
return i + a;
}
int minus(int a)
{
return i - a;
}
int delegate(int) get(char op)
{
int delegate(int) rval;
if (op == '+')
rval = &plus;
else if (op == '-')
rval = &minus;
return rval;
}
}
int calldg1(int delegate(int) dg, int i)
{
return dg(i);
}
void delegate() retdg()
{
void delegate() dg;
return dg;
}
void getretdg()
{
void delegate() dg;
dg = retdg();
}
class C
{
int i;
void m()
{
i = 42;
}
}
void getclassdg()
{
scope c = new C;
void delegate() dg = &c.m;
assert(c.i != 42);
dg();
assert(c.i == 42);
}
void main()
{
printf("Delegate test\n");
S s = S(4);
auto dg = &s.square;
//assert(dg() == 16);
//dg();
/*auto dg1 = &s.plus;
assert(dg1(6) == 10);
auto dg2 = &s.minus;
assert(calldg1(dg2,30) == -26);
auto dg3 = s.get('+');
assert(dg3(16) == 20);
getretdg();
getclassdg();*/
printf(" SUCCESS\n");
}

34
test/dotproduct.d Normal file
View File

@@ -0,0 +1,34 @@
struct vec3
{
float x,y,z;
float dot(ref vec3 v)
{
return x*v.x + y*v.y + z*v.z;
}
void print(char* n)
{
printf("%s = vec3(%.4f, %.4f, %.4f)\n", n, x,y,z);
}
}
int main()
{
printf("Dot Product test\n");
const float f = 0.7071067811865474617f;
vec3 v = vec3(f,f,0);
vec3 w = vec3(f,0,f);
v.print("v");
v.print("w");
auto dp = v.dot(w);
printf("v · w = %f\n", dp);
assert(dp > 0.4999 && dp < 0.5001);
printf(" SUCCESS\n");
return 0;
}

23
test/e.d Normal file
View File

@@ -0,0 +1,23 @@
module e;
struct C
{
float x=0,y=0;
float dot(ref C b)
{
return x*b.x + y*b.y;
}
}
void main()
{
C a,b;
a.x = 2;
a.y = 6;
b.x = 3;
b.y = 5;
float f = a.dot(b);
printf("%f\n", f);
assert(f == 36);
}

11
test/f.d Normal file
View File

@@ -0,0 +1,11 @@
module f;
struct S
{
long l;
}
void main()
{
S s;
}

160
test/floatcmp.d Normal file
View File

@@ -0,0 +1,160 @@
module floatcmp;
void eq()
{
float _3 = 3;
assert(!(_3 == 4));
assert(!(_3 == 2));
assert(_3 == 3);
assert(!(_3 == float.nan));
}
void neq()
{
float _3 = 3;
assert(_3 != 4);
assert(_3 != 2);
assert(!(_3 != 3));
assert(_3 != float.nan);
}
void gt()
{
float _3 = 3;
assert(_3 > 2);
assert(!(_3 > 4));
assert(!(_3 > 3));
assert(!(_3 > float.nan));
}
void ge()
{
float _3 = 3;
assert(_3 >= 2);
assert(!(_3 >= 4));
assert(_3 >= 3);
assert(!(_3 >= float.nan));
}
void lt()
{
float _3 = 3;
assert(_3 < 4);
assert(!(_3 < 2));
assert(!(_3 < 3));
assert(!(_3 < float.nan));
}
void le()
{
float _3 = 3;
assert(_3 <= 4);
assert(!(_3 <= 2));
assert(_3 <= 3);
assert(!(_3 <= float.nan));
}
void uno()
{
float _3 = 3;
assert(!(_3 !<>= 2));
assert(!(_3 !<>= 3));
assert(!(_3 !<>= 4));
assert(_3 !<>= float.nan);
}
void lg()
{
float _3 = 3;
assert(_3 <> 4);
assert(_3 <> 2);
assert(!(_3 <> 3));
assert(!(_3 <> float.nan));
}
void lge()
{
float _3 = 3;
assert(_3 <>= 4);
assert(_3 <>= 2);
assert(_3 <>= 3);
assert(!(_3 <>= float.nan));
}
void ugt()
{
float _3 = 3;
assert(_3 !<= 2);
assert(!(_3 !<= 4));
assert(!(_3 !<= 3));
assert(_3 !<= float.nan);
}
void uge()
{
float _3 = 3;
assert(_3 !< 2);
assert(!(_3 !< 4));
assert(_3 !< 3);
assert(_3 !< float.nan);
}
void ult()
{
float _3 = 3;
assert(_3 !>= 4);
assert(!(_3 !>= 2));
assert(!(_3 !>= 3));
assert(_3 !>= float.nan);
}
void ule()
{
float _3 = 3;
assert(_3 !> 4);
assert(!(_3 !> 2));
assert(_3 !> 3);
assert(_3 !> float.nan);
}
void ueq()
{
float _3 = 3;
assert(!(_3 !<> 2));
assert(!(_3 !<> 4));
assert(_3 !<> 3);
assert(_3 !<> float.nan);
}
void main()
{
printf("floating point comparison test\n");
eq();
neq();
gt();
ge();
lt();
le();
uno();
lg();
lge();
ugt();
uge();
ult();
ule();
ueq();
printf(" SUCCESS\n");
}
/+
void gt()
{
float _3 = 3;
assert();
assert();
assert();
assert();
}
+/

11
test/forwdecl.d Normal file
View File

@@ -0,0 +1,11 @@
struct S
{
S* s;
S** ss;
S*[4] s4;
}
void main()
{
S s;
}

66
test/funcptr.d Normal file
View File

@@ -0,0 +1,66 @@
int return_six()
{
return 6;
}
int add_int(int a, int b)
{
return a+b;
}
int sub_int(int a, int b)
{
return a-b;
}
alias int function(int,int) binfn_t;
int binop_int(binfn_t op, int a, int b)
{
return op(a,b);
}
binfn_t get_binop_int(char op)
{
binfn_t fn;
if (op == '+')
fn = &add_int;
else if (op == '-')
fn = &sub_int;
return fn;
}
extern(C) float mul_float(float a, float b)
{
return a * b;
}
void function_pointers()
{
int function() fn = &return_six;
assert(fn() == 6);
binfn_t binfn = &add_int;
assert(binfn(4,1045) == 1049);
assert(binop_int(binfn, 10,656) == 666);
binfn = get_binop_int('+');
assert(binop_int(binfn, 10,100) == 110);
binfn = get_binop_int('-');
assert(binop_int(binfn, 10,100) == -90);
{
auto ffn = &mul_float;
float ftmp = mul_float(2.5,5);
assert(ftmp == 12.5);
assert(ftmp > 12.49 && ftmp < 12.51);
}
}
void main()
{
printf("Function pointer test\n");
function_pointers();
printf(" SUCCESS\n");
}

48
test/funcs.d Normal file
View File

@@ -0,0 +1,48 @@
void main()
{
printf("Testing functions\n");
int i = 5;
assert(a(i) == 110);
assert(i == 11);
S s;
s.i = 5;
d(s);
assert(s.i == 5);
e(s);
assert(s.i == 6);
printf(" SUCCESS\n");
}
int a(ref int i)
{
i*=2;
return b(i);
}
int b(ref int i)
{
i++;
return c(i);
}
int c(int i)
{
return i*10;
}
struct S
{
int i;
}
void d(S s)
{
s.i++;
}
void e(ref S s)
{
s.i++;
}

11
test/imports_1of2.d Normal file
View File

@@ -0,0 +1,11 @@
module imports_1of2;
import imports_2of2;
void main()
{
assert(func() == 42);
S s;
s.l = 32;
assert(s.l == 32);
}

11
test/imports_2of2.d Normal file
View File

@@ -0,0 +1,11 @@
module imports_2of2;
int func()
{
return 42;
}
struct S
{
long l;
}

30
test/intrinsics.d Normal file
View File

@@ -0,0 +1,30 @@
import llvm.intrinsic;
extern(C) int scanf(char*,...);
void main()
{
{
float f;
printf("Enter float: ");
scanf("%f", &f);
float sf = llvm_sqrt(f);
printf("sqrt(%f) = %f\n", f, sf);
}
{
double d;
printf("Enter double: ");
scanf("%lf", &d);
double sd = llvm_sqrt(d);
printf("sqrt(%lf) = %lf\n", d, sd);
}
{
real d;
printf("Enter real: ");
scanf("%lf", &d);
real sd = llvm_sqrt(d);
printf("sqrt(%lf) = %lf\n", d, sd);
}
}

22
test/pointers.d Normal file
View File

@@ -0,0 +1,22 @@
module pointers;
struct S
{
long l;
}
void main()
{
int j = 42;
int* p = &j;
auto t = *p;
*p ^= t;
*p = ~t;
S s;
S* sp = &s;
*sp = s;
s = *sp;
}

28
test/pt.d Normal file
View File

@@ -0,0 +1,28 @@
int main()
{
char[16] s = void;
{
char[] sd = s;
{
s[0] = 'a';
s[1] = 'b';
s[2] = 'c';
}
printf("%p %p\n", s.ptr, sd.ptr);
printf("%c%c%c\n", s[0], s[1], s[2]);
}
char[16] s1 = void;
char[16] s2 = void;
char[] d1 = s1;
{
printf("%p\n%p\n%p\n", s1.ptr, s2.ptr, d1.ptr);
}
int[16] arr;
return 0;
}

33
test/ptrarith.d Normal file
View File

@@ -0,0 +1,33 @@
void main()
{
printf("Pointer arithmetic test\n");
int* p;
printf("0x%x\n", p);
assert(p++ is null);
assert(cast(size_t)p == 4);
printf("0x%x\n", p);
p--;
assert(p is null);
printf("0x%x\n", p);
int d = 4;
p+=d;
printf("0x%x\n", p);
assert(cast(size_t)p == 16);
d = 2;
p+=d;
printf("0x%x\n", p);
assert(cast(size_t)p == 0x18);
d = 6;
p-=d;
printf("0x%x\n", p);
assert(p is null);
printf(" SUCCESS\n");
}
void fill_byte_array(ubyte* a, size_t n, ubyte v)
{
auto p = a;
auto end = a+n;
while (p !is end)
*p++ = v;
}

31
test/sieve.d Normal file
View File

@@ -0,0 +1,31 @@
/* Eratosthenes Sieve prime number calculation. */
bool flags[8191];
int main()
{ int i, prime, k, count, iter;
printf("10 iterations\n");
for (iter = 1;
iter <= 10;
iter++)
{
count = 0;
flags[] = true;
for (i = 0; i < flags.length; i++)
{ if (flags[i])
{
prime = i + i + 3;
k = i + prime;
while (k < flags.length)
{
flags[k] = false;
k += prime;
}
count += 1;
}
}
}
printf("%d primes\n", count);
return 0;
}

34
test/slices.d Normal file
View File

@@ -0,0 +1,34 @@
module slices;
void main()
{
//char[] a = "hello world";
//char[5] b = a[0..5];
//char* cp = a.ptr;
//char[] c = cp[0..1];
}
char[] first5(char[] str)
{
char* p = str.ptr;
return p[0..5];
}
int[] one()
{
static int i;
return (&i)[0..1];
}
void[] init()
{
static char c;
return (&c)[0 .. 1];
}
void[] init2()
{ static char c;
return (cast(char *)&c)[0 .. 1];
}

14
test/static_ctor.d Normal file
View File

@@ -0,0 +1,14 @@
static this()
{
printf("static this\n");
}
static ~this()
{
printf("static ~this\n");
}
void main()
{
printf("main\n");
}

70
test/staticarrays.d Normal file
View File

@@ -0,0 +1,70 @@
void numbers()
{
bool[8] bools;
char[8] chars;
byte[8] bytes;
short[8] shorts;
int[8] ints;
long[8] longs;
float[8] floats;
double[8] doubles;
real[8] reals;
{
bools[7] = true;
floats[7] = 3.14159265;
{
printf("bools[0] = %d, bools[7] = %d\n", bools[0], bools[7]);
printf("floats[0] = %f, floats[7] = %f\n", floats[0], floats[7]);
}
}
}
struct S
{
int i = 42;
void print()
{
printf("S.i = %d\n", i);
}
}
class C
{
int i;
this()
{
i = 3;
}
void print()
{
printf("C.i = %d\n", i);
}
}
void refs()
{
void*[5] voids;
S*[5] structs;
C[5] classes;
{
voids[0] = cast(void*)0xA;
printf("void* = %p\n", voids[0]);
}
{
structs[0] = new S;
structs[0].print();
delete structs[0];
}
{
classes[0] = new C;
classes[0].print();
delete classes[0];
}
}
void main()
{
numbers();
refs();
}

24
test/structinit.d Normal file
View File

@@ -0,0 +1,24 @@
module structinit;
import structinit2;
struct S
{
uint ui;
float f;
long l;
real r;
}
S adef;
S a = { 1, 2.0f };
S b = { f:2.0f, l:42 };
Imp imp;
void main()
{
//assert(a == S.init);
//assert(b == S(0,3.14f,0,real.init));
}

8
test/structinit2.d Normal file
View File

@@ -0,0 +1,8 @@
module structinit2;
struct Imp
{
int i;
long l;
float f;
}

13
test/structs.d Normal file
View File

@@ -0,0 +1,13 @@
struct S
{
int func()
{
return 42;
}
}
void main()
{
S s;
int i = s.func();
}

13
test/structs2.d Normal file
View File

@@ -0,0 +1,13 @@
module structs2;
struct S
{
int i;
char c;
S* s;
char[4] ca;
}
void main()
{
}

45
test/terms.d Normal file
View File

@@ -0,0 +1,45 @@
module terms;
void f()
{
int i = 42;
if (i > 25)
return;
if (i < 25) {
return;
}
if (i > 100)
return;
else
return;
if (i < 1000) {
return;
}
else {
return;
}
}
void main()
{
int i = 42;
if (i > 25)
return;
if (i < 25) {
return;
}
if (i > 100)
return;
else
return;
if (i < 1000) {
return;
}
else {
return;
}
}

9
test/typeinfo.d Normal file
View File

@@ -0,0 +1,9 @@
module typeinfo;
typedef int int_t;
void main()
{
int_t i;
i += 3;
}

30
test/v2d.d Normal file
View File

@@ -0,0 +1,30 @@
struct V2D(T)
{
T x,y;
T dot(ref V2D v)
{
return x*v.x + y*v.y;
}
V2D opAdd(ref V2D v)
{
return V2D(x+v.x, y+v.y);
}
}
alias V2D!(float) V2Df;
void main()
{
printf("V2D test\n");
auto up = V2Df(0.0f, 1.0f);
auto right = V2Df(1.0f, 0.0f);
assert(up.dot(right) == 0.0f);
auto upright = up + right;
assert(upright.x == 1.0f && upright.y == 1.0f);
auto copy = upright;
copy.x++;
assert(copy.x > upright.x);
printf(" SUCCESS\n");
}

15
test/virtcall.d Normal file
View File

@@ -0,0 +1,15 @@
module virtcall;
class C
{
override char[] toString()
{
return "overridden";
}
}
void main()
{
C c = new C;
auto s = c.toString();
}