Skip to content

Commit

Permalink
CTFE Test cases for pointer arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
Don Clugston committed Jun 9, 2011
1 parent de33d38 commit 23e4b60
Showing 1 changed file with 177 additions and 0 deletions.
177 changes: 177 additions & 0 deletions test/compilable/interpret3.d
Expand Up @@ -1314,3 +1314,180 @@ static assert(!is(typeof(compiles!({
return true;
}()
))));

/**************************************************
Bug 6053 -- ICE involving pointers
**************************************************/

static assert({
int *a = null;
assert(a is null);
assert(a == null);
return true;
}());

static assert({
int b;
int* a= &b;
assert(a !is null);
*a = 7;
assert(b==7);
assert(*a == 7);
return true;
}());

int dontbreak6053()
{
auto q = &dontbreak6053;
void caz() {}
auto tr = &caz;
return 5;
}
static assert(dontbreak6053());

static assert({
int a; *(&a) = 15;
assert(a==15);
assert(*(&a)==15);
return true;
}());

static assert({
int a=5, b=6, c=2;
assert( *(c ? &a : &b) == 5);
assert( *(!c ? &a : &b) == 6);
return true;
}());

static assert({
int a, b, c; (c ? a : b) = 1;
return true;
}());

static assert({
int a, b, c=1;
int *p=&a; (c ? *p : b) = 51;
assert(a==51);
return true;
}());

/**************************************************
Pointer arithmetic, dereference, and comparison
**************************************************/

// dereference null pointer
static assert(!is(typeof(compiles!({
int a, b, c=1; int *p; (c ? *p : b) = 51; return 6;
}()
))));
static assert(!is(typeof(compiles!({
int *a = null; assert(*a!=6); return 72;
}()
))));

// cannot <, > compare pointers to different arrays
static assert(!is(typeof(compiles!({
int a[5]; int b[5]; bool c = (&a[0] > &b[0]);
return 72;
}()
))));

// can ==, is,!is,!= compare pointers for different arrays
static assert({
int a[5]; int b[5];
assert(!(&a[0] == &b[0]));
assert(&a[0] != &b[0]);
assert(!(&a[0] is &b[0]));
assert(&a[0] !is &b[0]);
return 72;
}());

static assert({
int a[5];
a[0] = 25;
a[1] = 5;
int *b = &a[1];
assert(*b == 5);
*b = 34;
int c = *b;
*b += 6;
assert(b == &a[1]);
assert(b != &a[0]);
assert(&a[0] < &a[1]);
assert(&a[0] <= &a[1]);
assert(!(&a[0] >= &a[1]));
assert(&a[4] > &a[0]);
assert(c==34);
assert(*b ==40);
assert(a[1] == 40);
return true;
}());

static assert({
int [12] x;
int *p = &x[10];
int *q = &x[4];
return p-q;
}() == 6);

static assert({
int [12] x;
int *p = &x[10];
int *q = &x[4];
q = p;
assert(p == q);
q = &x[4];
assert(p != q);
q = q + 6;
assert(q is p);
return 6;
}() == 6);

static assert({
int [12] x;
int [] y = x[2..8];
int *p = &y[4];
int *q = &x[6];
assert(p == q);
p = &y[5];
assert(p > q);
p = p + 5; // OK, as long as we don't dereference
assert(p > q);
return 6;
}() == 6);

static assert({
char [12] x;
const(char) *p = "abcdef";
const (char) *q = p;
q = q + 2;
assert(*q == 'c');
assert(q > p);
assert(q - p == 2);
assert(p - q == -2);
q = &x[7];
p = &x[1];
assert(q>p);
return 6;
}() == 6);

/**************************************************
Out-of-bounds pointer assignment and deference
**************************************************/

int ptrDeref(int ofs, bool wantDeref)
{
int a[5];
int *b = &a[0];
b = b + ofs; // OK
if (wantDeref)
return *b; // out of bounds
return 72;
}

static assert(!is(typeof(compiles!(ptrDeref(-1, true)))));
static assert( is(typeof(compiles!(ptrDeref(4, true)))));
static assert( is(typeof(compiles!(ptrDeref(5, false)))));
static assert(!is(typeof(compiles!(ptrDeref(5, true)))));
static assert(!is(typeof(compiles!(ptrDeref(6, false)))));
static assert(!is(typeof(compiles!(ptrDeref(6, true)))));

0 comments on commit 23e4b60

Please sign in to comment.