From 23e4b606e63e5f6838018819f2d7df6c311e8345 Mon Sep 17 00:00:00 2001 From: Don Clugston Date: Thu, 2 Jun 2011 02:54:30 +0200 Subject: [PATCH] CTFE Test cases for pointer arithmetic --- test/compilable/interpret3.d | 177 +++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) diff --git a/test/compilable/interpret3.d b/test/compilable/interpret3.d index d0050597fc32..0f37060ef829 100644 --- a/test/compilable/interpret3.d +++ b/test/compilable/interpret3.d @@ -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)))));