-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
1669 lines (1558 loc) · 40.3 KB
/
test.c
File metadata and controls
1669 lines (1558 loc) · 40.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
int printf();
char* section_name;
int section_count;
int is_fall;
int section(char* name) {
section_name = name;
section_count = 0;
}
int assert(int expected, int actual, char* msg) {
section_count = section_count + 1;
if(expected != actual) {
printf("*** Assertion failed: %s\n", msg);
printf(" Expected: %d\n", expected);
printf(" Actual: %d\n", actual);
char* ordinal;
if(section_count % 10 == 1)
ordinal = "st";
else if(section_count % 10 == 2)
ordinal = "nd";
else if(section_count % 10 == 3)
ordinal = "rd";
else
ordinal = "th";
if(section_count == 11) ordinal = "th";
if(section_count == 12) ordinal = "th";
if(section_count == 13) ordinal = "th";
printf(" Test for the %d%s %s\n\n", section_count, ordinal, section_name);
is_fall = 1;
return 1;
}
}
int return_value_1() {
return 0;
}
int return_value_2() {
return 42;
}
int return_value() {
section("値を返す");
assert(0, return_value_1(), "return_value_1");
assert(42, return_value_2(), "return_value_2");
}
int four_arithmetic() {
section("四則演算");
assert(3, 1 + 2, "1 + 2");
assert(0, 1 + 2 - 3, "1 + 2 - 3");
assert(1, 1 - 2 + 3 + 4 - 5, "1 - 2 + 3 + 4 - 5");
assert(41, 1 + 20 * 2, "1 + 20 * 2");
assert(9, (20 - 1) / 2, "(20 - 1) / 2");
}
int monominal() {
section("単項+, 単項-");
assert(10, -10 + 20, "-10 + 20");
assert(10, +10, "+10");
assert(10, - -10, "- -10");
}
int compare() {
section("比較演算子");
assert(0, 2 == 3, "2 == 3");
assert(1, 2 != 3, "2 != 3");
assert(1, 2 <= 2, "2 <= 2");
assert(0, 2 < 2, "2 < 2");
assert(1, 2 >= 2, "2 >= 2");
assert(0, 2 > 2, "2 > 2");
}
int one_char_variable_1() {
int a;
a = 1;
return 1;
}
int one_char_variable_2() {
int a;
a = 1;
return a;
}
int one_char_variable_3() {
int a;
a = 1;
int b;
b = 2;
return a + b;
}
int one_char_variable() {
section("一文字変数");
assert(1, one_char_variable_1(), "one_char_variable_1");
assert(1, one_char_variable_2(), "one_char_variable_2");
assert(3, one_char_variable_3(), "one_char_variable_3");
}
int multi_char_variable_1() {
int abc;
abc = 12;
return abc;
}
int multi_char_variable_2() {
int _;
_ = 3;
_ = _ * 10;
return _ + 4;
}
int multi_char_variable() {
section("複数文字, 特殊文字変数");
assert(12, multi_char_variable_1(), "multi_char_variable_1");
assert(34, multi_char_variable_2(), "multi_char_variable_2");
}
int return_statement_1() {
return 10;
}
int return_statement_2() {
return 10;
20;
}
int return_statement_3() {
return 10;
return 20;
}
int return_statement() {
section("return文");
assert(10, return_statement_1(), "return_statement_1");
assert(10, return_statement_2(), "return_statement_2");
assert(10, return_statement_3(), "return_statement_3");
}
int if_statement_1() {
if(1)
return 10;
}
int if_statement_2() {
if(0)
return 10;
return 20;
}
int if_statement_3() {
if(0)
return 10;
return 20;
}
int if_statement_4() {
if(0)
return 10;
else
return 20;
}
int if_statement_5() {
if(0)
return 10;
else if(1)
return 20;
else
return 30;
return 40;
}
int if_statement_6() {
if(0)
return 10;
else if(0)
return 20;
else
return 30;
return 40;
}
int if_statement() {
section("if文");
assert(10, if_statement_1(), "if_statement_1");
assert(20, if_statement_2(), "if_statement_2");
assert(20, if_statement_3(), "if_statement_3");
assert(20, if_statement_4(), "if_statement_4");
assert(20, if_statement_5(), "if_statement_5");
assert(30, if_statement_6(), "if_statement_6");
}
int while_statement_1() {
while(0) return 12;
return 34;
}
int while_statement_2() {
while(1) return 56;
return 78;
}
int while_statement_3() {
int a;
a = 0;
while(a < 5) a = a + 1;
return a;
}
int while_statement_4() {
int a;
a = 0;
while(1) if (a == 5) return a; else a = a + 1;
}
int while_statement() {
section("while文");
assert(34, while_statement_1(), "while_statement_1");
assert(56, while_statement_2(), "while_statement_2");
assert(5, while_statement_3(), "while_statement_3");
assert(5, while_statement_4(), "while_statement_4");
}
int for_statement_1() {
for(;;) return 3;
}
int for_statement_2() {
int a;
a = 0;
for(;;) if(a == 5) return a; else a = a + 1;
}
int for_statement_3() {
int a;
for(a = 0; a < 9; a = a + 1) return 4;
}
int for_statement_4() {
int a;
for(a = 0; a < 9; a = a + 1) 1;
return a;
}
int for_statement_5() {
int a;
for(a = 9;;) if(a == 9) return a;
}
int for_statement_6() {
int a;
for(a = 0; a < 9; a = a + 1) if(a == 5) return a;
}
int for_statement_7() {
int sum;
sum = 0;
int i;
for(i = 0; i <= 10; i = i + 1) sum = sum + i;
return sum;
}
int for_statement() {
section("for文");
assert(3, for_statement_1(), "for_statement_1");
assert(5, for_statement_2(), "for_statement_2");
assert(4, for_statement_3(), "for_statement_3");
assert(9, for_statement_4(), "for_statement_4");
assert(9, for_statement_5(), "for_statement_5");
assert(5, for_statement_6(), "for_statement_6");
assert(55, for_statement_7(), "for_statement_7");
}
int block_statement_1() {
{
return 10;
}
}
int block_statement_2() {
{
return 10;
20;
}
}
int block_statement_3() {
{
10;
return 20;
}
}
int block_statement_4() {
{
int a;
a = 10;
return a;
}
}
int block_statement_5() {
if(1) {
return 20;
}
}
int block_statement_6() {
if(1) {
int a;
a = 10;
return a;
}
}
int block_statement_7() {
if(0) {
int a;
a = 10;
return a;
} else {
int b;
b = 20;
return b;
}
}
int block_statement_8() {
int a;
a = 0;
while(1) {
a = a + 10;
if(a == 30) {
return a;
}
}
}
int block_statement_9() {
int a;
for(a = 0; a < 100; a = a + 10) {
if(a == 40) {
return a;
}
}
}
int block_statement() {
section("ブロック文");
assert(10, block_statement_1(), "block_statement_1");
assert(10, block_statement_2(), "block_statement_2");
assert(20, block_statement_3(), "block_statement_3");
assert(10, block_statement_4(), "block_statement_4");
assert(20, block_statement_5(), "block_statement_5");
assert(10, block_statement_6(), "block_statement_6");
assert(20, block_statement_7(), "block_statement_7");
assert(30, block_statement_8(), "block_statement_8");
assert(40, block_statement_9(), "block_statement_9");
}
int function_prototype_func();
int function_prototype_1arg1(int);
int function_prototype_1arg2(int b);
int function_prototype_2arg1(int a, int b);
int function_prototype_2arg2(int x, int); // xは実際の定義とは異なる名前
int *function_prototype_pointer(int *a);
int function_prototype_1() {
return function_prototype_func();
}
int function_prototype_2() {
return function_prototype_1arg1(10);
}
int function_prototype_3() {
return function_prototype_1arg2(10);
}
int function_prototype_4() {
return function_prototype_2arg1(2, 8);
}
int function_prototype_5() {
return function_prototype_2arg2(2, 8);
}
int function_prototype_6() {
int a;
a = 10;
return *function_prototype_pointer(&a);
}
int function_prototype_func() {
return 10;
}
int function_prototype_1arg1(int a) {
return a;
}
int function_prototype_1arg2(int b) {
return b;
}
int function_prototype_2arg1(int a, int b) {
return a + b;
}
int function_prototype_2arg2(int a, int b) {
return a + b;
}
int *function_prototype_pointer(int *a) {
return a;
}
int function_prototype() {
section("関数プロトタイプ");
assert(10, function_prototype_1(), "function_prototype_1");
assert(10, function_prototype_2(), "function_prototype_2");
assert(10, function_prototype_3(), "function_prototype_3");
assert(10, function_prototype_4(), "function_prototype_4");
assert(10, function_prototype_5(), "function_prototype_5");
assert(10, function_prototype_6(), "function_prototype_6");
}
int call0();
int call1(int);
int call2(int, int);
int call3(int, int, int);
int call4(int, int, int, int);
int call5(int, int, int, int, int);
int call6(int, int, int, int, int, int);
int function_call_with_0arg_1() {
return call0();
}
int function_call_with_0arg_2() {
return call0() + 10;
}
int function_call_with_0arg_3() {
call0();
return call0() + call0();
}
int function_call_with_0arg_4() {
int a;
a = call0();
return a;
}
int function_call_with_0arg_5() {
{
call0();
return call0();
}
}
int function_call_with_0arg() {
section("0引数関数呼び出し");
assert(100, function_call_with_0arg_1(), "function_call_with_0arg_1");
assert(110, function_call_with_0arg_2(), "function_call_with_0arg_2");
assert(200, function_call_with_0arg_3(), "function_call_with_0arg_3");
assert(100, function_call_with_0arg_4(), "function_call_with_0arg_4");
assert(100, function_call_with_0arg_5(), "function_call_with_0arg_5");
}
int function_call_with_1arg_1() {
return call1(10);
}
int function_call_with_1arg_2() {
call1(10);
return call1(10) * 2;
}
int function_call_with_1arg_3() {
int a;
a = call1(10);
return a;
}
int function_call_with_1arg_4() {
if(1)
return call1(10);
else
return call1(20);
}
int function_call_with_1arg() {
section("1引数関数呼び出し");
assert(110, function_call_with_1arg_1(), "function_call_with_1arg_1");
assert(220, function_call_with_1arg_2(), "function_call_with_1arg_2");
assert(110, function_call_with_1arg_3(), "function_call_with_1arg_3");
assert(110, function_call_with_1arg_4(), "function_call_with_1arg_4");
}
int function_call_with_multiarg_1() {
return call2(10, 20);
}
int function_call_with_multiarg_2() {
return call3(10, 20, 30);
}
int function_call_with_multiarg_3() {
return call4(10, 20, 30, 39);
}
int function_call_with_multiarg_4() {
return call5(1, 2, 3, 4, 5);
}
int function_call_with_multiarg_5() {
return call6(1, 2, 3, 4, 5, 6);
}
int function_call_with_multiarg_6() {
return call2(3, 5 + 5);
}
int function_call_with_multiarg_7() {
return call2(5 * 2, 3);
}
int function_call_with_multiarg() {
section("複数引数関数呼び出し");
assert(30, function_call_with_multiarg_1(), "function_call_with_multiarg_1");
assert(60, function_call_with_multiarg_2(), "function_call_with_multiarg_2");
assert(99, function_call_with_multiarg_3(), "function_call_with_multiarg_3");
assert(15, function_call_with_multiarg_4(), "function_call_with_multiarg_4");
assert(21, function_call_with_multiarg_5(), "function_call_with_multiarg_5");
assert(13, function_call_with_multiarg_6(), "function_call_with_multiarg_6");
assert(13, function_call_with_multiarg_7(), "function_call_with_multiarg_7");
}
int function_definition_with_0arg_nobody() {
return 0;
}
int function_definition_with_0arg_return() {
return 42;
}
int function_definition_with_0arg_call_other_A() {
return 42;
}
int function_definition_with_0arg_call_other_B() {
return function_definition_with_0arg_call_other_A();
}
int function_definition_with_0arg_variable() {
int a;
a = 42;
return a;
}
int function_definition_with_0arg() {
section("0引数関数定義");
assert(0, function_definition_with_0arg_nobody(), "function_definition_with_0arg_nobody");
assert(42, function_definition_with_0arg_return(), "function_definition_with_0arg_return");
assert(42, function_definition_with_0arg_call_other_B(), "function_definition_with_0arg_call_other_B");
assert(42, function_definition_with_0arg_variable(), "function_definition_with_0arg_variable");
}
int function_definition_with_multiarg_id(int a) {
return a;
}
int function_definition_with_multiarg_add(int a, int b) {
return a + b;
}
int function_definition_with_multiarg_fib(int n) {
if(n == 0) return 0;
if(n == 1) return 1;
return function_definition_with_multiarg_fib(n - 1) + function_definition_with_multiarg_fib(n - 2);
}
int function_definition_with_multiarg() {
section("複数引数関数定義");
assert(13, function_definition_with_multiarg_id(13), "function_definition_with_multiarg_id");
assert(13, function_definition_with_multiarg_add(3, 10), "function_definition_with_multiarg_add");
assert(13, function_definition_with_multiarg_fib(7), "function_definition_with_multiarg_fib");
}
int addr_and_deref_1() {
int a;
a = 42;
int *p;
p = &a;
return *p;
}
int addr_and_deref() {
section("単項&, 単項*");
assert(42, addr_and_deref_1(), "addr_and_deref_1");
}
int one_level_pointer_1() {
int a;
a = 42;
int *p;
p = &a;
return *p;
}
int one_level_pointer_2() {
int a;
int *b;
b = &a;
*b = 42;
return a;
}
int one_level_pointer_deref(int *p) {
return *p;
}
int one_level_pointer_3() {
int a;
a = 42;
return one_level_pointer_deref(&a);
}
int one_level_pointer_setref(int *p) {
*p = 42;
return 0;
}
int one_level_pointer_4() {
int a;
one_level_pointer_setref(&a);
return a;
}
int one_level_pointer_add(int *a, int *b) {
return *a + *b;
}
int one_level_pointer_5() {
int a;
a = 20;
int b;
b = 22;
return one_level_pointer_add(&a, &b);
}
int one_level_pointer() {
section("一重ポインタ");
assert(42, one_level_pointer_1(), "one_level_pointer_1");
assert(42, one_level_pointer_2(), "one_level_pointer_2");
assert(42, one_level_pointer_3(), "one_level_pointer_3");
assert(42, one_level_pointer_4(), "one_level_pointer_4");
assert(42, one_level_pointer_5(), "one_level_pointer_5");
}
int multi_level_pointer_1() {
int a;
a = 42;
int *p;
p = &a;
int **q;
q = &p;
return **q;
}
int multi_level_pointer_2() {
int a;
int *b;
int **c;
b = &a;
c = &b;
**c = 42;
return a;
}
int multi_level_pointer() {
section("多重ポインタ");
assert(42, multi_level_pointer_1(), "multi_level_pointer_1");
assert(42, multi_level_pointer_2(), "multi_level_pointer_2");
}
int *alloc_int(int, int, int, int);
int pointer_add() {
section("ポインタ加算");
int *a;
a = alloc_int(1, 2, 3, 4);
assert(2, *(a + 1), "*(a + 1)");
assert(3, *(a + 2), "*(a + 2)");
assert(2, *(a + 3 - 2), "*(a + 3 - 2)");
}
int sizeof_one_level_pointer() {
section("一重ポインタのsizeof演算子");
assert(4, sizeof(1), "sizeof(1)");
assert(4, sizeof 1 , "sizeof 1");
int a;
assert(4, sizeof(a), "sizeof(a)");
int *p;
assert(8, sizeof(p), "sizeof(p)");
assert(4, sizeof(sizeof(1)), "sizeof(sizeof(1))");
}
int sizeof_multi_level_pointer() {
section("多重ポインタのsizeof演算子");
int **a;
assert(8, sizeof(a), "sizeof(a)");
assert(8, sizeof(a + 4), "sizeof(a + 4)");
assert(8, sizeof(*a), "sizeof(*a)");
}
int array_declaration() {
section("配列宣言");
int a[1];
*a = 2;
assert(2, *a, "*a");
int b[2];
*b = 3;
assert(3, *b, "*b");
}
int sizeof_array() {
section("配列のsizeof演算子");
int a[1];
assert(4, sizeof(a), "sizeof(a)");
int b[2];
assert(8, sizeof(b), "sizeof(b)");
int *c[3];
assert(24, sizeof(c), "sizeof(c)");
int **d[4];
assert(32, sizeof(d), "sizeof(d)");
}
int array_index_access() {
section("配列の添字アクセス");
int a[3];
*a = 1;
assert(1, a[0], "a[0]");
a[0] = 2;
assert(2, *a, "*a");
a[1] = 3;
assert(3, a[1], "a[1]");
a[1] = 4;
assert(4, *(a + 1), "*(a + 1)");
a[1] = 5;
int* p;
p = a;
assert(5, *(p + 1), "*(p + 1)");
(a)[1] = 6;
assert(6, (a)[1], "(a)[1]");
1[a] = 7;
assert(7, 1[a], "1[a]");
int *b[3];
int n;
n = 8;
b[1] = &n;
assert(8, *b[1], "*b[1]");
}
int global_variable_a;
int* global_variable_pointer;
int global_variable_array[3];
int global_variable_set() {
global_variable_a = 1;
}
int global_variable_get() {
return global_variable_a;
}
int global_variable_conflict() {
int global_variable_a;
global_variable_a = 2;
}
int global_variable() {
section("グローバル変数");
assert(0, global_variable_a, "global_variable_a");
assert(0, global_variable_pointer, "global_variable_pointer");
assert(0, global_variable_array[0], "global_variable_array[0]");
assert(0, global_variable_array[1], "global_variable_array[1]");
assert(0, global_variable_array[2], "global_variable_array[2]");
global_variable_set();
assert(1, global_variable_a, "global_variable_a");
assert(1, global_variable_get(), "global_variable_get");
int b;
global_variable_pointer = &b;
b = 2;
assert(2, *global_variable_pointer, "*global_variable_pointer");
global_variable_array[1] = 3;
assert(3, global_variable_array[1], "global_variable_array[1]");
global_variable_conflict();
assert(1, global_variable_a, "global_variable_a");
int* p;
p = &global_variable_a;
*p = 2;
assert(2, global_variable_a, "global_variable_a");
}
int string_type() {
section("文字列型");
char a;
a = 1;
assert(1, a, "a");
char* b;
b = &a;
*b = 2;
assert(2, a, "a");
char c[3];
c[0] = 3;
c[1] = 4;
c[2] = 5;
assert(4, c[1], "c[1]");
char d;
d = 6;
assert(8, a + d, "a + d");
assert(4, d - a, "d - a");
assert(12, a * d, "a * d");
assert(3, d / a, "d / a");
assert(0, a == d, "a == d");
assert(1, a != d, "a != d");
assert(1, a < d, "a < d");
assert(0, a > d, "a > d");
assert(1, a <= d, "a <= d");
assert(0, a >= d, "a >= d");
}
int string_literal() {
section("文字列リテラル");
char* a;
a = "abc";
assert(97, a[0], "a[0]");
assert(98, a[1], "a[1]");
assert(99, a[2], "a[2]");
assert(0, a[3], "a[3]");
a = "";
assert(0, a[0], "a[0]");
a = "abc";
assert(97, a[0], "a[0]");
assert(98, a[1], "a[1]");
assert(99, a[2], "a[2]");
assert(0, a[3], "a[3]");
}
int comment() {
section("コメント");
// コメント
/*
assert(1, 0, "コメント");
*/
}
int sizeof_char() {
section("文字型のsizeof演算子");
char a;
assert(1, sizeof(a), "sizeof(a)");
char* b;
assert(8, sizeof(b), "sizeof(b)");
char c[3];
assert(3, sizeof(c), "sizeof(c)");
}
int modulo_operator() {
section("剰余演算子");
assert(1, 10 % 3, "10 % 3");
assert(2, 10 % 4, "10 % 4");
assert(0, 10 % 5, "10 % 5");
}
struct struct_definition_sample {
int a;
int b;
};
int struct_definition() {
section("構造体の定義");
struct struct_definition_sample s;
s.a = 1;
s.b = 2;
assert(1, s.a, "s.a");
assert(2, s.b, "s.b");
assert(3, s.a + s.b, "s.a + s.b");
}
struct struct_definition_sample2 {
int a;
struct struct_definition_sample b;
};
int multi_level_struct() {
section("多段階の構造体");
struct struct_definition_sample2 s;
s.a = 1;
s.b.a = 2;
s.b.b = 3;
assert(1, s.a, "s.a");
assert(2, s.b.a, "s.b.a");
assert(4, s.a + s.b.b, "s.a + s.b.b");
}
int arrow_operator() {
section("アロー演算子");
struct struct_definition_sample s;
struct struct_definition_sample* p;
p = &s;
p->a = 1;
p->b = 2;
assert(1, s.a, "s.a");
assert(2, s.b, "s.b");
}
struct struct_definition_list {
int head;
struct struct_definition_list* tail;
};
int* malloc(int);
int multi_level_arrow_operator() {
section("多段階のアロー演算子");
struct struct_definition_list a;
struct struct_definition_list b;
a.head = 1;
a.tail = &b;
b.head = 2;
b.tail = 0;
assert(1, a.head, "a.head");
assert(2, a.tail->head, "a.tail->head");
assert(0, a.tail->tail, "a.tail->tail");
a.tail->tail = malloc(8);
a.tail->tail->head = 3;
a.tail->tail->tail = 0;
assert(3, a.tail->tail->head, "a.tail->tail->head");
assert(0, a.tail->tail->tail, "a.tail->tail->tail");
}
int sizeof_type_name() {
section("型名のsizeof演算子");
// パディングに対応していないので、複雑な構造体はテストしない
assert(1, sizeof(char), "sizeof(char)");
assert(4, sizeof(int), "sizeof(int)");
assert(8, sizeof(int*), "sizeof(int*)");
assert(8, sizeof(char*), "sizeof(char*)");
assert(8, sizeof(int**), "sizeof(int**)");
assert(8, sizeof(struct struct_definition_sample), "sizeof(struct struct_definition_sample)");
assert(8, sizeof(struct struct_definition_sample*), "sizeof(struct struct_definition_sample*)");
}
enum enum_definition_sample {
ENUM_DEFINITION_SAMPLE_A,
ENUM_DEFINITION_SAMPLE_B,
ENUM_DEFINITION_SAMPLE_C
};
int enum_definition() {
section("列挙型の定義");
assert(0, ENUM_DEFINITION_SAMPLE_A, "ENUM_DEFINITION_SAMPLE_A");
assert(1, ENUM_DEFINITION_SAMPLE_B, "ENUM_DEFINITION_SAMPLE_B");
assert(2, ENUM_DEFINITION_SAMPLE_C, "ENUM_DEFINITION_SAMPLE_C");
enum enum_definition_sample e;
e = ENUM_DEFINITION_SAMPLE_A;
assert(0, e, "e");
e = ENUM_DEFINITION_SAMPLE_B;
assert(1, e, "e");
e = ENUM_DEFINITION_SAMPLE_C;
assert(2, e, "e");
}
typedef int typedef_int;
typedef char typedef_char;
typedef int* typedef_int_pointer;
typedef struct struct_definition_sample typedef_struct;
typedef struct struct_definition_sample* typedef_struct_pointer;
typedef enum enum_definition_sample typedef_enum;
typedef enum enum_definition_sample* typedef_enum_pointer;
int typedef_type() {
section("typedefの定義");
typedef_int a;
a = 1;
assert(1, a, "a");
typedef_char b;
b = 2;
assert(2, b, "b");
typedef_int_pointer c;
int d;
c = &d;
*c = 3;
assert(3, d, "d");
typedef_struct e;
e.a = 4;
e.b = 5;
assert(4, e.a, "e.a");
assert(5, e.b, "e.b");
typedef_struct_pointer f;
struct struct_definition_sample g;
f = &g;
f->a = 6;
f->b = 7;
assert(6, g.a, "g.a");
assert(7, g.b, "g.b");
typedef_enum h;
h = ENUM_DEFINITION_SAMPLE_A;
assert(0, h, "h");
typedef_enum_pointer i;
enum enum_definition_sample j;
i = &j;
*i = ENUM_DEFINITION_SAMPLE_B;
assert(1, j, "j");
}
int sizeof_typedef() {
section("typedefのsizeof演算子");
assert(4, sizeof(typedef_int), "sizeof(typedef_int)");
assert(1, sizeof(typedef_char), "sizeof(typedef_char)");
assert(8, sizeof(typedef_int_pointer), "sizeof(typedef_int_pointer)");
assert(8, sizeof(typedef_struct), "sizeof(typedef_struct)");
assert(8, sizeof(typedef_struct_pointer), "sizeof(typedef_struct_pointer)");
assert(4, sizeof(typedef_enum), "sizeof(typedef_enum)");
assert(8, sizeof(typedef_enum_pointer), "sizeof(typedef_enum_pointer)");
}
extern int extern_definition_sample;
int extern_definition() {
section("外部変数の定義");
extern_definition_sample = 1;
assert(1, extern_definition_sample, "extern_definition_sample");
}
int extern_definition_sample;
int local_declaration_assignment() {
section("ローカル変数の宣言時代入");
int a = 1;
assert(1, a, "a");
char b = 2;
assert(2, b, "b");
int* c = &a;
assert(1, *c, "*c");
}
int string_escape() {
section("文字列エスケープ");
char* a;
a = "\n";
assert(10, a[0], "a[0]");
assert(0, a[1], "a[1]");
a = "\t";
assert(9, a[0], "a[0]");
assert(0, a[1], "a[1]");
a = "\"";
assert(34, a[0], "a[0]");
assert(0, a[1], "a[1]");
a = "\\";
assert(92, a[0], "a[0]");
assert(0, a[1], "a[1]");
a = "\0";
assert(0, a[0], "a[0]");
// strlenを使っている箇所でNULL文字以降がコピーされているのか、うまく動かない
// assert(0, a[1], "a[1]");
}
int char_literal() {
section("文字リテラル");
char a;
a = 'a';
assert(97, a, "a");
a = '\n';
assert(10, a, "a");
a = '\t';
assert(9, a, "a");
a = '\'';
assert(39, a, "a");
a = '\\';