44
55typedef struct point point ;
66
7- // Composite literals passed to function-like C macros (len, cap, append,
8- // copy, clear, indexing, slicing, map access) emit braced initializers whose
9- // commas would be misread by the preprocessor as macro argument separators.
10- // Each such argument must be wrapped in parentheses.
117typedef struct point {
128 so_int x ;
139 so_int y ;
@@ -16,64 +12,18 @@ typedef struct point {
1612// -- Implementation --
1713
1814int main (void ) {
19- // len/cap of a slice literal.
20- if (so_len (((so_Slice ){(so_int [3 ]){1 , 2 , 3 }, 3 , 3 })) != 3 ) {
21- so_panic ("len" );
22- }
23- if (so_cap (((so_Slice ){(so_int [3 ]){1 , 2 , 3 }, 3 , 3 })) != 3 ) {
24- so_panic ("cap" );
25- }
26- // index and slice of a slice literal.
27- if (so_at (so_int , ((so_Slice ){(so_int [3 ]){10 , 20 , 30 }, 3 , 3 }), 1 ) != 20 ) {
28- so_panic ("index" );
29- }
30- if (so_len (so_slice (so_int , ((so_Slice ){(so_int [4 ]){1 , 2 , 3 , 4 }, 4 , 4 }), 1 , 3 )) != 2 ) {
31- so_panic ("slice" );
32- }
33- if (so_len (so_slice (so_int , ((so_Slice ){(so_int [4 ]){1 , 2 , 3 , 4 }, 4 , 4 }), 2 , ((so_Slice ){(so_int [4 ]){1 , 2 , 3 , 4 }, 4 , 4 }).len )) != 2 ) {
34- so_panic ("slice open-ended" );
35- }
36- // address of an element of a slice literal.
37- (void )& so_at (so_int , ((so_Slice ){(so_int [3 ]){5 , 6 , 7 }, 3 , 3 }), 0 );
38- // slice-to-array conversion of a literal.
39- so_int arr [2 ];
40- memcpy (arr , so_slice_array (((so_Slice ){(so_int [2 ]){7 , 8 }, 2 , 2 }), 2 ), sizeof (arr ));
41- if (arr [0 ] != 7 ) {
42- so_panic ("slice-to-array" );
43- }
44- // byte slice literal to string conversion.
45- if (so_string_ne (so_bytes_string (((so_Slice ){(so_byte [2 ]){'h' , 'i' }, 2 , 2 })), so_str ("hi" ))) {
46- so_panic ("byte slice to string" );
47- }
48- if (so_string_ne (so_bytes_string (((so_Slice ){(so_byte [1 ]){(so_byte )(97 )}, 1 , 1 })), so_str ("a" ))) {
49- so_panic ("byte slice to string" );
50- }
51- // copy from a slice literal.
52- so_Slice dst = so_make_slice (so_int , 3 , 3 );
53- so_copy (so_int , dst , ((so_Slice ){(so_int [3 ]){1 , 2 , 3 }, 3 , 3 }));
54- if (so_at (so_int , dst , 2 ) != 3 ) {
55- so_panic ("copy" );
56- }
5715 // append a composite-literal value.
5816 so_Slice pts = so_make_slice (point , 0 , 2 );
5917 pts = so_append (point , pts , ((point ){1 , 2 }));
6018 if (so_at (point , pts , 0 ).y != 2 ) {
6119 so_panic ("append value" );
6220 }
63- // clear a slice literal (exercises the macro; no observable effect).
64- so_clear (so_int , ((so_Slice ){(so_int [3 ]){1 , 2 , 3 }, 3 , 3 }));
6521 // map with a composite-literal value.
6622 so_Map * mv = so_make_map (so_int , point , 1 );
6723 so_map_set (so_int , point , mv , 0 , ((point ){3 , 4 }));
6824 if (so_map_get (so_int , point , mv , 0 ).x != 3 ) {
6925 so_panic ("map value" );
7026 }
71- // map with a composite-literal pointer value.
72- so_Map * mp = so_make_map (so_int , point * , 1 );
73- so_map_set (so_int , point * , mp , 1 , (& (point ){8 , 9 }));
74- if (so_map_get (so_int , point * , mp , 1 )-> y != 9 ) {
75- so_panic ("map pointer value" );
76- }
7727 // map with a composite-literal key.
7828 so_Map * mk = so_make_map (point , so_int , 1 );
7929 so_map_set (point , so_int , mk , ((point ){1 , 2 }), 42 );
0 commit comments