Changes in tests/references.cfa [f498c51:dc8511c]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/references.cfa
rf498c51 rdc8511c 9 9 // Author : Rob Schluntz 10 10 // Created On : Wed Aug 23 16:11:50 2017 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Tue Dec 25 14:31:48 201813 // Update Count : 1111 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Aug 23 16:12:03 13 // Update Count : 2 14 14 // 15 15 16 #include <fstream.hfa>17 18 16 struct Y { int i; }; 19 void ?{}( Y & y ) { sout | "Default constructing a Y"; }20 void ?{}( Y & y, Y other ) { sout | "Copy constructing a Y"; }21 void ^?{}( Y & y ) { sout | "Destructing a Y"; }22 Y ?=?( Y & y, Y other ) { sout | "Assigning a Y"; return y; }23 void ?{}( Y & y, int i ) { sout | "Value constructing a Y" | i; y.i = i; }17 void ?{}(Y & y) { printf("Default constructing a Y\n"); } 18 void ?{}(Y & y, Y other) { printf("Copy constructing a Y\n"); } 19 void ^?{}(Y & y) { printf("Destructing a Y\n"); } 20 Y ?=?(Y & y, Y other) { printf("Assigning a Y\n"); return y; } 21 void ?{}(Y & y, int i) { printf("Value constructing a Y %d\n", i); y.i = i; } 24 22 25 23 struct X { Y & r; Y y; }; 26 void ?{}( X & x) {24 void ?{}(X & x) { 27 25 // ensure that r is not implicitly constructed 28 26 } 29 void ?{}( X & x, X other) {27 void ?{}(X & x, X other) { 30 28 // ensure that r is not implicitly constructed 31 29 } 32 void ^?{}( X & x) {30 void ^?{}(X & x) { 33 31 // ensure that r is not implicitly destructed 34 32 } 35 X ?=?( X & x, X other) { return x; }33 X ?=?(X & x, X other) { return x; } 36 34 37 35 // ensure that generated functions do not implicitly operate on references … … 50 48 int x = 123456, x2 = 789, *p1 = &x, **p2 = &p1, ***p3 = &p2, 51 49 &r1 = x, &&r2 = r1, &&&r3 = r2; 52 ***p3 = 3; 53 **p3 = &x; 54 *p3 = &p1; 55 int y = 0, z = 11, & ar[3] = { x, y, z }; 56 57 58 50 ***p3 = 3; // change x 51 **p3 = &x; // change p1 52 *p3 = &p1; // change p2 53 int y = 0, z = 11, & ar[3] = { x, y, z }; // initialize array of references 54 // &ar[1] = &z; // change reference array element 55 // typeof( ar[1] ) p = 3; // is int, i.e., the type of referenced object 56 // typeof( &ar[1] ) q = &x; // is int *, i.e., the type of pointer 59 57 // _Static_assert( sizeof( ar[1] ) == sizeof( int ), "Array type should be int." ); // is true, i.e., the size of referenced object 60 58 // _Static_assert( sizeof( &ar[1] ) == sizeof( int *), "Address of array should be int *." ); // is true, i.e., the size of a reference 61 59 62 ((int*&)&r3) = &x; 60 ((int*&)&r3) = &x; // change r1, (&*)**r3 63 61 x = 3; 64 62 // test that basic reference properties are true - r1 should be an alias for x 65 sout | x | r1 | &x == &r1;63 printf("%d %d %d\n", x, r1, &x == &r1); 66 64 r1 = 12; 67 sout | x | r1 | &x == &r1;65 printf("%d %d %d\n", x, r1, &x == &r1); 68 66 69 67 // test that functions using basic references work 70 sout | toref( &x ) | toref( p1 ) | toptr( r1 ) == toptr( x ) | toptr( r1 ) == &x;68 printf("%d %d %d %d\n", toref(&x), toref(p1), toptr(r1) == toptr(x), toptr(r1) == &x); 71 69 72 70 changeRef( x ); 73 71 changeRef( y ); 74 72 changeRef( z ); 75 sout | x | y | z;73 printf("%d %d %d\n", x, y, z); 76 74 changeRef( r1 ); 77 sout | r1 | x;75 printf("%d %d\n", r1, x); 78 76 79 r3 = 6; 80 sout | "x = " | x | " ; x2 = " | x2;// check that x was changed81 &r3 = &x2; 82 r3 = 999; 83 sout | "x = " | x | " ; x2 = " | x2;// check that x2 was changed84 ((int**&)&&r3) = p2; 85 r3 = 12345; 86 sout | "x = " | x | " ; x2 = " | x2;// check that x was changed87 &&&r3 = p3; 88 ((int&)r3) = 22222; 89 sout | "x = " | x | " ; x2 = " | x2;// check that x was changed77 r3 = 6; // change x, ***r3 78 printf("x = %d ; x2 = %d\n", x, x2); // check that x was changed 79 &r3 = &x2; // change r1 to refer to x2, (&*)**r3 80 r3 = 999; // modify x2 81 printf("x = %d ; x2 = %d\n", x, x2); // check that x2 was changed 82 ((int**&)&&r3) = p2; // change r2, (&(&*)*)*r3, ensure explicit cast to reference works 83 r3 = 12345; // modify x 84 printf("x = %d ; x2 = %d\n", x, x2); // check that x was changed 85 &&&r3 = p3; // change r3 to p3, (&(&(&*)*)*)r3 86 ((int&)r3) = 22222; // modify x, ensure explicit cast to reference works 87 printf("x = %d ; x2 = %d\n", x, x2); // check that x was changed 90 88 91 89 // test that reference members are not implicitly constructed/destructed/assigned … … 104 102 struct S { double x, y; }; 105 103 void f( int & i, int & j, S & s, int v[] ) { 106 sout | i | j | "{ " | s.[x, y] | " }," | "[" | v[0] | "," | v[1] | "," | v[2] | "]";104 printf("%d %d { %g, %g }, [%d, %d, %d]\n", i, j, s.[x, y], v[0], v[1], v[2]); 107 105 } 108 void g(int & i) { sout | i; }109 void h(int &&& i) { sout | i; }106 void g(int & i) { printf("%d\n", i); } 107 void h(int &&& i) { printf("%d\n", i); } 110 108 111 int &&& r = 3; 109 int &&& r = 3; // rvalue to reference 112 110 int i = r; 113 sout | i | r;// both 3111 printf("%d %d\n", i, r); // both 3 114 112 115 g( 3 ); 116 h( (int &&&)3 ); 113 g( 3 ); // rvalue to reference 114 h( (int &&&)3 ); // rvalue to reference 117 115 118 116 int a = 5, b = 4;
Note: See TracChangeset
for help on using the changeset viewer.