xref: /illumos-gate/usr/src/cmd/dtrace/test/tst/common/pointers/tst.ArrayPointer1.d (revision 73b80b821da76082173547dd650e417f6916eb8b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * ASSERTION: Declare arrays of different data types and verify that the
29  * addresses of the individual elements differ by an amount equal to the number
30  * elements separating them multiplied by the size of each element.
31  *
32  * SECTION: Pointers and Arrays/Array Declarations and Storage;
33  * 	Pointers and Arrays/Pointer Arithmetic
34  *
35  * NOTES:
36  *
37  */
38 
39 #pragma D option quiet
40 
41 char char_array[5];
42 short short_array[5];
43 int int_array[5];
44 long long_array[5];
45 long long long_long_array[5];
46 int8_t int8_array[5];
47 int16_t int16_array[5];
48 int32_t int32_array[5];
49 int64_t int64_array[5];
50 intptr_t intptr_array[5];
51 uint8_t uint8_array[5];
52 uint16_t uint16_array[5];
53 uint32_t uint32_array[5];
54 uint64_t uint64_array[5];
55 uintptr_t uintptr_array[5];
56 
57 /*
58 float float_array[5];
59 double double_array[5];
60 long double long_double_array[5];
61 
62 string string_array[5];
63 */
64 
65 struct record {
66 	char ch;
67 	int in;
68 } struct_array[5];
69 
70 struct {
71 	char ch;
72 	int in;
73 } anon_struct_array[5];
74 
75 union record {
76 	char ch;
77 	int in;
78 } union_array[5];
79 
80 union {
81 	char ch;
82 	int in;
83 } anon_union_array[5];
84 
85 enum colors {
86 	RED,
87 	GREEN,
88 	BLUE
89 } enum_array[5];
90 
91 BEGIN
92 {
93 	char_var0 = &char_array[0]; char_var2 = &char_array[2];
94 	short_var0 = &short_array[0]; short_var3 = &short_array[3];
95 	int_var3 = &int_array[3]; int_var5 = &int_array[5];
96 
97 	long_var0 = &long_array[0]; long_var4 = &long_array[4];
98 	long_long_var0 = &long_long_array[0];
99 	long_long_var2 = &long_long_array[2];
100 	int8_var3 = &int8_array[3]; int8_var5 = &int8_array[5];
101 
102 	int16_var0 = &int16_array[0]; int16_var4 = &int16_array[4];
103 	int32_var0 = &int32_array[0]; int32_var3 = &int32_array[3];
104 	int64_var0 = &int64_array[0]; int64_var1 = &int64_array[1];
105 
106 	uintptr_var0 = &uintptr_array[0]; uintptr_var2 = &uintptr_array[2];
107 	struct_var0 = &struct_array[0]; struct_var2 = &struct_array[2];
108 	anon_struct_var3 = &anon_struct_array[3];
109 	anon_struct_var5 = &anon_struct_array[5];
110 
111 	union_var0 = &union_array[0]; union_var3 = &union_array[3];
112 	anon_union_var0 = &anon_union_array[0];
113 	anon_union_var4 = &anon_union_array[4];
114 	enum_var0 = &enum_array[0]; enum_var2 = &enum_array[2];
115 
116 	printf("char_var2 - char_var0: %d\n",
117 	(int) char_var2 - (int) char_var0);
118 	printf("short_var3 - short_var0: %d\n",
119 	(int) short_var3 - (int) short_var0);
120 	printf("int_var5 - int_var3: %d\n", (int) int_var5 - (int) int_var3);
121 
122 	printf("long_var4 - long_var0: %d\n",
123 	(int) long_var4 - (int) long_var0);
124 	printf("long_long_var2 - long_long_var0: %d\n",
125 	(int) long_long_var2 - (int) long_long_var0);
126 	printf("int8_var5 - int8_var3: %d\n",
127 	(int) int8_var5 - (int) int8_var3);
128 
129 	printf("int16_var4 - int16_var0: %d\n",
130 	(int) int16_var4 - (int) int16_var0);
131 	printf("int32_var3 - int32_var0: %d\n",
132 	(int) int32_var3 - (int) int32_var0);
133 	printf("int64_var1 - int64_var0: %d\n",
134 	(int) int64_var1 - (int) int64_var0);
135 
136 	printf("uintptr_var2 - uintptr_var0: %d\n",
137 	(int) uintptr_var2 - (int) uintptr_var0);
138 	printf("struct_var2 - struct_var0: %d\n",
139 	(int) struct_var2 - (int) struct_var0);
140 	printf("anon_struct_var5 - anon_struct_var3: %d\n",
141 	(int) anon_struct_var5 - (int) anon_struct_var3);
142 
143 	printf("union_var3 - union_var0: %d\n",
144 	(int) union_var3 - (int) union_var0);
145 	printf("anon_union_var4 - anon_union_var0: %d\n",
146 	(int) anon_union_var4 - (int) anon_union_var0);
147 	printf("enum_var2 - enum_var0: %d\n",
148 	(int) enum_var2 - (int) enum_var0);
149 	exit(0);
150 }
151 
152 END
153 /(2 != ((int) char_var2 - (int) char_var0)) ||
154     (6 != ((int) short_var3 - (int) short_var0)) ||
155     (8 != ((int) int_var5 - (int) int_var3)) ||
156     ((32 != ((int) long_var4 - (int) long_var0)) &&
157     (16 != ((int) long_var4 - (int) long_var0))) ||
158     (16 != ((int) long_long_var2 - (int) long_long_var0)) ||
159     (2 != ((int) int8_var5 - (int) int8_var3))
160     || (8 != ((int) int16_var4 - (int) int16_var0)) ||
161     (12 != ((int) int32_var3 - (int) int32_var0)) ||
162     (8 != ((int) int64_var1 - (int) int64_var0)) ||
163     ((16 != ((int) uintptr_var2 - (int) uintptr_var0))
164     && (8 != ((int) uintptr_var2 - (int) uintptr_var0))) ||
165     (16 != ((int) struct_var2 - (int) struct_var0)) ||
166     (16 != ((int) anon_struct_var5 - (int) anon_struct_var3))
167     || (12 != ((int) union_var3 - (int) union_var0)) ||
168     (16 != ((int) anon_union_var4 - (int) anon_union_var0)) ||
169     (8 != ((int) enum_var2 - (int) enum_var0))/
170 {
171 	exit(1);
172 }
173