source: src/tests/polymorphism.c@ c0d00b6

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since c0d00b6 was 3787dc1, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Update polymorphism test to include aggregates with polymorphic members

  • Property mode set to 100644
File size: 2.2 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 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// polymorphism.c --
8//
9// Author : Rob Schluntz
10// Created On : Tue Oct 17 12:19:48 2017
11// Last Modified By : Rob Schluntz
12// Last Modified On : Tue Oct 17 12:21:07 2017
13// Update Count : 1
14//
15
16#include <assert.h>
17#include <inttypes.h>
18
19forall(otype T)
20T f(T x, T y) {
21 x = y;
22 return x;
23}
24
25forall(otype T) T ident(T x) {
26 return x;
27}
28
29forall( otype T, otype U )
30size_t struct_size( T i, U j ) {
31 struct S { T i; U j; };
32 return sizeof(S);
33}
34
35forall( otype T, otype U )
36size_t union_size( T i, U j ) {
37 union B { T i; U j; };
38 return sizeof(B);
39}
40
41// perform some simple operations on aggregates of T and U
42forall( otype T | { void print(T); int ?==?(T, T); }, otype U | { void print(U); U ?=?(U&, zero_t); } )
43U foo(T i, U j) {
44 struct S { T i; U j; };
45 union B { T i; U j; };
46
47 S s;
48 s.i = i;
49 assert(s.i == i);
50
51 B b;
52 b.j = 0;
53 b.i = s.i;
54 return b.j;
55}
56
57int main() {
58 {
59 // ensure that x is not changed by the invocation of a polymorphic function
60 int x = 123;
61 int y = 456;
62 int z = f(x, y);
63 printf("%d %d %d\n", x, y, z);
64 }
65
66 {
67 // explicitly specialize function
68 int (*f)(int) = ident;
69 ((int(*)(int))ident);
70 printf("%d %d\n", f(5), ((int(*)(int))ident)(5));
71 }
72
73 {
74 // test aggregates with polymorphic members
75 typedef uint32_t x_type;
76 typedef uint64_t y_type;
77
78 x_type x = 3;
79 y_type y = 3;
80
81 struct S {
82 x_type f1;
83 y_type f2;
84 };
85 union U {
86 x_type f1;
87 y_type f2;
88 };
89 // ensure that the size of aggregates with polymorphic members
90 // matches the size of the aggregates in a monomorphic context
91 assert( struct_size(x, y) == sizeof(S) );
92 assert( union_size(x, y) == sizeof(U) );
93
94 y_type ?=?(y_type & this, zero_t) {
95 this = (int)0;
96 return this;
97 }
98
99 void print(x_type x) {
100 printf("%"PRIu32"\n", x);
101 }
102
103 void print(y_type y) {
104 printf("%"PRIu64"\n", y);
105 }
106
107 y_type ret = foo(x, y);
108
109 // duplicate logic from inside of foo to ensure the same results
110 U u;
111 u.f2 = 0;
112 u.f1 = x;
113 assert(ret == u.f2);
114 }
115}
116
117// Local Variables: //
118// tab-width: 4 //
119// End: //
Note: See TracBrowser for help on using the repository browser.