source: src/tests/references.c@ af1ed1ad

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since af1ed1ad was 12db9e4, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Update references test to include rvalue-to-reference conversion

  • Property mode set to 100644
File size: 3.5 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// references.c --
8//
9// Author : Rob Schluntz
10// Created On : Wed Aug 23 16:11:50 2017
11// Last Modified By : Rob Schluntz
12// Last Modified On : Wed Aug 23 16:12:03
13// Update Count : 2
14//
15
16struct Y { int i; };
17void ?{}(Y & y) { printf("Default constructing a Y\n"); }
18void ?{}(Y & y, Y other) { printf("Copy constructing a Y\n"); }
19void ^?{}(Y & y) { printf("Destructing a Y\n"); }
20Y ?=?(Y & y, Y other) { printf("Assigning a Y\n"); return y; }
21void ?{}(Y & y, int i) { printf("Value constructing a Y %d\n", i); y.i = i; }
22
23struct X { Y & r; Y y; };
24void ?{}(X & x) {
25 // ensure that r is not implicitly constructed
26}
27void ?{}(X & x, X other) {
28 // ensure that r is not implicitly constructed
29}
30void ^?{}(X & x) {
31 // ensure that r is not implicitly destructed
32}
33X ?=?(X & x, X other) { return x; }
34
35// ensure that generated functions do not implicitly operate on references
36struct Z { Y & r; Y y; };
37
38// test user-defined reference-returning function
39int & toref( int * p ) { return *p; }
40// test user-defined reference-parameter function
41int * toptr( int & r ) { return &r; }
42
43void changeRef( int & r ) {
44 r++;
45}
46
47int main() {
48 int x = 123456, *p1 = &x, **p2 = &p1, ***p3 = &p2,
49 &r1 = x, &&r2 = r1, &&&r3 = r2;
50 ***p3 = 3; // change x
51 // ((int&)r3 = 3; // change x, ***r3
52 **p3 = &x; // change p1
53 // ((int*&)&r3) = &x; // change r1, (&*)**r3
54 *p3 = &p1; // change p2
55 // ((int**&)&&r3) = &p2; // change r2, (&(&*)*)*r3
56 // ((int***&)&&&r3) = p3; // change r3 to p3, (&(&(&*)*)*)r3
57 int y = 0, z = 11, & ar[3] = { x, y, z }; // initialize array of references
58 // &ar[1] = &z; // change reference array element
59 // typeof( ar[1] ) p; // is int, i.e., the type of referenced object
60 // typeof( &ar[1] ) q; // is int &, i.e., the type of reference
61 // sizeof( ar[1] ) == sizeof( int ); // is true, i.e., the size of referenced object
62 // sizeof( &ar[1] ) == sizeof( int *); // is true, i.e., the size of a reference
63
64 // test that basic reference properties are true - r1 should be an alias for x
65 printf("%d %d %d\n", x, r1, &x == &r1);
66 r1 = 12;
67 printf("%d %d %d\n", x, r1, &x == &r1);
68
69 // test that functions using basic references work
70 printf("%d %d %d %d\n", toref(&x), toref(p1), toptr(r1) == toptr(x), toptr(r1) == &x);
71
72 changeRef( x );
73 changeRef( y );
74 changeRef( z );
75 printf("%d %d %d\n", x, y, z);
76 changeRef( r1 );
77 printf("%d %d\n", r1, x);
78
79 // test that reference members are not implicitly constructed/destructed/assigned
80 X x1, x2 = x1;
81 x1 = x2;
82
83 Z z1, z2 = z1;
84 Y z1r = 56, z2r = 78;
85 &z1.r = &z1r;
86 &z2.r = &z2r;
87
88 z1 = z2;
89
90 // test rvalue-to-reference conversion
91 {
92 struct S { double x, y; };
93 void f( int & i, int & j, S & s, int v[] ) {
94 printf("%d %d { %g, %g }, [%d, %d, %d]\n", i, j, s.[x, y], v[0], v[1], v[2]);
95 }
96 void g(int & i) { printf("%d\n", i); }
97 void h(int &&& i) { printf("%d\n", i); }
98
99 int &&& r = 3; // rvalue to reference
100 int i = r;
101 printf("%d %d\n", i, r); // both 3
102
103 g( 3 ); // rvalue to reference
104 h( (int &&&)3 ); // rvalue to reference
105
106 int a = 5, b = 4;
107 f( 3, a + b, (S){ 1.0, 7.0 }, (int [3]){ 1, 2, 3 } ); // two rvalue to reference
108 }
109}
110
111// Local Variables: //
112// tab-width: 4 //
113// End: //
114
Note: See TracBrowser for help on using the repository browser.