# source:src/tests/references.c@a8555c5

Last change on this file since a8555c5 was a8555c5, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Start of writing a reference test

• Property mode set to `100644`
File size: 1.9 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
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
39int main() {
40        int x = 123456, *p1 = &x, **p2 = &p1, ***p3 = &p2,
41                &r1 = x,    &&r2 = r1,   &&&r3 = r2;
42        ***p3 = 3;                          // change x
43        **p3 = &x;                          // change p1
44        *p3 = &p1;                          // change p2
45        int y, z, & ar[3] = { x, y, z };    // initialize array of references
46
47        // test that basic reference properties are true - r1 should be an alias for x
48        printf("%d %d %d\n", x, r1, &x == &r1);
49        r1 = 12;
50        printf("%d %d %d\n", x, r1, &x == &r1);
51
52        // test that functions using basic references work
53        printf("%d %d %d %d\n", toref(&x), toref(p1), toptr(r1) == toptr(x), toptr(r1) == &x);
54
55        // test that reference members are not implicitly constructed/destructed/assigned
56        X x1, x2 = x1;
57        x1 = x2;
58}
59
60// Local Variables: //
61// tab-width: 4 //
62// End: //
63
Note: See TracBrowser for help on using the repository browser.