source: src/tests/references.c@ bfd4974

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 resolv-new with_gc
Last change on this file since bfd4974 was b05a4eb, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Update references test

  • Property mode set to 100644
File size: 2.0 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; }
21
22struct X { Y & r; Y y; };
23void ?{}(X & x) {
24 // ensure that r is not implicitly constructed
25}
26void ?{}(X & x, X other) {
27 // ensure that r is not implicitly constructed
28}
29void ^?{}(X & x) {
30 // ensure that r is not implicitly destructed
31}
32X ?=?(X & x, X other) { return x; }
33
34// test user-defined reference-returning function
35int & toref( int * p ) { return *p; }
36// test user-defined reference-parameter function
37int * toptr( int & r ) { return &r; }
38
39void changeRef( int & r ) {
40 r++;
41}
42
43int main() {
44 int x = 123456, *p1 = &x, **p2 = &p1, ***p3 = &p2,
45 &r1 = x, &&r2 = r1, &&&r3 = r2;
46 ***p3 = 3; // change x
47 **p3 = &x; // change p1
48 *p3 = &p1; // change p2
49 int y = 0, z = 11, & ar[3] = { x, y, z }; // initialize array of references
50
51 // test that basic reference properties are true - r1 should be an alias for x
52 printf("%d %d %d\n", x, r1, &x == &r1);
53 r1 = 12;
54 printf("%d %d %d\n", x, r1, &x == &r1);
55
56 // test that functions using basic references work
57 printf("%d %d %d %d\n", toref(&x), toref(p1), toptr(r1) == toptr(x), toptr(r1) == &x);
58
59 changeRef( x );
60 changeRef( y );
61 changeRef( z );
62 printf("%d %d %d\n", x, y, z);
63 changeRef( r1 );
64 printf("%d %d\n", r1, x);
65
66 // test that reference members are not implicitly constructed/destructed/assigned
67 X x1, x2 = x1;
68 x1 = x2;
69}
70
71// Local Variables: //
72// tab-width: 4 //
73// End: //
74
Note: See TracBrowser for help on using the repository browser.