-
Notifications
You must be signed in to change notification settings - Fork 71
/
test_layout.c
974 lines (784 loc) · 28.9 KB
/
test_layout.c
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
#include <stdio.h>
#define LAY_IMPLEMENTATION
#include "layout.h"
#ifdef _WIN32
#include <windows.h>
LONG WINAPI LayTestUnhandledExceptionFilter(EXCEPTION_POINTERS *ExceptionInfo)
{
fputs("Exception", stdout);
fflush(stdout);
// TODO Do useful stuff
return EXCEPTION_EXECUTE_HANDLER;
}
#endif
/*
int sprint_vec4(char* str, lay_vec4 v)
{
return sprintf(str, "%d, %d, %d, %d", v[0], v[1], v[2], v[3]);
}
int print_vec4(lay_vec4 v)
{
return printf("%d, %d, %d, %d", v[0], v[1], v[2], v[3]);
}
int sprint_vec2(char* str, lay_vec2 v)
{
return sprintf(str, "%d, %d", v[0], v[1]);
}
int sprint_item_info(char* str, lay_context *ctx, lay_id item)
{
int offs = 0;
lay_vec4 item_rect = lay_get_rect(ctx, item);
lay_vec4 item_margins = lay_get_margins(ctx, item);
lay_vec2 item_size = lay_get_size(ctx, item);
offs += sprintf(str + offs, "rect: ");
offs += sprint_vec4(str + offs, item_rect);
offs += sprintf(str + offs, " | margins: ");
offs += sprint_vec4(str + offs, item_margins);
offs += sprintf(str + offs, " | size: ");
offs += sprint_vec2(str + offs, item_size);
return offs;
}
*/
#define LTEST_TRUE(cond) \
if (cond) {} \
else { \
printf("Failed test at line %d in %s\n", __LINE__, __func__); \
fflush(stdout); \
abort(); \
}
#define LTEST_FALSE(cond) \
if (cond) { \
printf("Failed test at line %d in %s\n", __LINE__, __func__); \
fflush(stdout); \
abort(); \
} \
else {}
#define LTEST_DECLARE(testname) \
static void test_##testname(lay_context *ctx)
// vec4 equals test
#define LTEST_VEC4EQ(vecvar, x, y, z, w) \
LTEST_TRUE(vecvar[0] == x && vecvar[1] == y && vecvar[2] == z && vecvar[3] == w)
#define LTEST_VEC4UNEQ(vecvar, x, y, z, w) \
LTEST_FALSE(vecvar[0] == x && vecvar[1] == y && vecvar[2] == z && vecvar[3] == w)
LTEST_DECLARE(testing_sanity)
{
// MSVC (rightly) warns us that these are constant expressions which are
// always or never true.
#ifndef _MSC_VER
LTEST_TRUE(1);
LTEST_TRUE(-1);
LTEST_TRUE(((bool)(1)));
LTEST_TRUE(!((bool)(0)));
LTEST_TRUE(34518 == 34518);
LTEST_TRUE(1337 != 420);
LTEST_FALSE(0);
LTEST_FALSE(!((bool)(1)));
LTEST_FALSE(((bool)(0)));
LTEST_FALSE(34518 != 34518);
LTEST_FALSE(1337 == 420);
#endif
lay_vec4 v1 = lay_vec4_xyzw(56, 57, 58, 59);
LTEST_VEC4EQ(v1, 56, 57, 58, 59);
LTEST_VEC4UNEQ(v1, 55, 57, 58, 59);
LTEST_VEC4UNEQ(v1, 55, 57, 58, 59);
LTEST_TRUE(ctx != NULL);
}
LTEST_DECLARE(simple_fill)
{
lay_id root = lay_item(ctx);
lay_id child = lay_item(ctx);
lay_set_size_xy(ctx, root, 30, 40);
lay_set_behave(ctx, child, LAY_FILL);
lay_insert(ctx, root, child);
lay_run_context(ctx);
lay_vec4 root_r = lay_get_rect(ctx, root);
lay_vec4 child_r = lay_get_rect(ctx, child);
LTEST_TRUE(root_r[0] == 0 && root_r[1] == 0);
LTEST_TRUE(root_r[2] == 30 && root_r[3] == 40);
LTEST_TRUE(child_r[0] == 0 && child_r[1] == 0);
LTEST_TRUE(child_r[2] == 30 && child_r[3] == 40);
// Test to make sure size is ok
lay_vec2 root_size = lay_get_size(ctx, root);
// Make sure _xy version gives the same values
lay_scalar root_size_cs[2];
lay_get_size_xy(ctx, root, &root_size_cs[0], &root_size_cs[1]);
LTEST_TRUE(root_size[0] == 30 && root_size[1] == 40);
LTEST_TRUE(root_size[0] == root_size_cs[0] && root_size[1] == root_size_cs[1]);
// Test to make sure the _xywh getter produces the same results as the
// lay_vec4-return-value version.
lay_scalar root_r_cs[4];
lay_scalar child_r_cs[4];
lay_get_rect_xywh(ctx, root, &root_r_cs[0], &root_r_cs[1], &root_r_cs[2], &root_r_cs[3]);
lay_get_rect_xywh(ctx, child, &child_r_cs[0], &child_r_cs[1], &child_r_cs[2], &child_r_cs[3]);
LTEST_TRUE(root_r[0] == root_r_cs[0] && root_r[1] == root_r_cs[1] && root_r[2] == root_r_cs[2] && root_r[3] == root_r_cs[3]);
LTEST_TRUE(child_r[0] == child_r_cs[0] && child_r[1] == child_r_cs[1] && child_r[2] == child_r_cs[2] && child_r[3] == child_r_cs[3]);
}
LTEST_DECLARE(reserve_capacity)
{
lay_reserve_items_capacity(ctx, 512);
LTEST_TRUE(lay_items_capacity(ctx) >= 512);
// Run some simple stuff like above, just to make sure it's still working
lay_id root = lay_item(ctx);
lay_id child = lay_item(ctx);
lay_set_size_xy(ctx, root, 30, 40);
lay_set_behave(ctx, child, LAY_FILL);
lay_insert(ctx, root, child);
lay_run_context(ctx);
lay_vec4 root_r = lay_get_rect(ctx, root);
lay_vec4 child_r = lay_get_rect(ctx, child);
LTEST_TRUE(root_r[0] == 0 && root_r[1] == 0);
LTEST_TRUE(root_r[2] == 30 && root_r[3] == 40);
LTEST_TRUE(child_r[0] == 0 && child_r[1] == 0);
LTEST_TRUE(child_r[2] == 30 && child_r[3] == 40);
LTEST_TRUE(lay_items_capacity(ctx) >= 512);
}
LTEST_DECLARE(multiple_uninserted)
{
lay_id root = lay_item(ctx);
lay_id child1 = lay_item(ctx);
lay_id child2 = lay_item(ctx);
lay_set_size_xy(ctx, root, 155, 177);
lay_set_size_xy(ctx, child2, 1, 1);
lay_run_context(ctx);
lay_vec4 root_r, child1_r, child2_r;
root_r = lay_get_rect(ctx, root);
child1_r = lay_get_rect(ctx, child1);
child2_r = lay_get_rect(ctx, child2);
LTEST_VEC4EQ(root_r, 0, 0, 155, 177);
// This uninserted child should obviously be zero:
LTEST_VEC4EQ(child1_r, 0, 0, 0, 0);
// You might expect this to pass for this child:
//
// LTEST_VEC4EQ(child2_r, 0, 0, 1, 1);
//
// But it won't, because items not inserted into the root will not have
// their output rect set. (Hmm, is this a good API design idea?)
//
// Instead, it will be zero:
LTEST_VEC4EQ(child2_r, 0, 0, 0, 0);
}
LTEST_DECLARE(column_even_fill)
{
lay_id root = lay_item(ctx);
lay_id child_a = lay_item(ctx);
lay_id child_b = lay_item(ctx);
lay_id child_c = lay_item(ctx);
lay_set_size_xy(ctx, root, 50, 60);
lay_set_contain(ctx, root, LAY_COLUMN);
lay_set_behave(ctx, child_a, LAY_FILL);
lay_set_behave(ctx, child_b, LAY_FILL);
lay_set_behave(ctx, child_c, LAY_FILL);
lay_set_size_xy(ctx, child_a, 0, 0);
lay_set_size_xy(ctx, child_b, 0, 0);
lay_set_size_xy(ctx, child_c, 0, 0);
lay_insert(ctx, root, child_a);
lay_insert(ctx, root, child_b);
lay_insert(ctx, root, child_c);
lay_run_context(ctx);
LTEST_VEC4EQ(lay_get_rect(ctx, root), 0, 0, 50, 60);
LTEST_VEC4EQ(lay_get_rect(ctx, child_a), 0, 0, 50, 20);
LTEST_VEC4EQ(lay_get_rect(ctx, child_b), 0, 20, 50, 20);
LTEST_VEC4EQ(lay_get_rect(ctx, child_c), 0, 40, 50, 20);
}
LTEST_DECLARE(row_even_fill)
{
lay_id root = lay_item(ctx);
lay_id child_a = lay_item(ctx);
lay_id child_b = lay_item(ctx);
lay_id child_c = lay_item(ctx);
lay_set_size_xy(ctx, root, 90, 3);
lay_set_contain(ctx, root, LAY_ROW);
lay_set_behave(ctx, child_a, LAY_HFILL | LAY_TOP);
lay_set_behave(ctx, child_b, LAY_HFILL | LAY_VCENTER);
lay_set_behave(ctx, child_c, LAY_HFILL | LAY_BOTTOM);
lay_set_size_xy(ctx, child_a, 0, 1);
lay_set_size_xy(ctx, child_b, 0, 1);
lay_set_size_xy(ctx, child_c, 0, 1);
lay_insert(ctx, root, child_a);
lay_insert(ctx, root, child_b);
lay_insert(ctx, root, child_c);
lay_run_context(ctx);
LTEST_VEC4EQ(lay_get_rect(ctx, root), 0, 0, 90, 3);
LTEST_VEC4EQ(lay_get_rect(ctx, child_a), 0, 0, 30, 1);
LTEST_VEC4EQ(lay_get_rect(ctx, child_b), 30, 1, 30, 1);
LTEST_VEC4EQ(lay_get_rect(ctx, child_c), 60, 2, 30, 1);
}
LTEST_DECLARE(fixed_and_fill)
{
lay_id root = lay_item(ctx);
lay_id fixed_a = lay_item(ctx);
lay_id fixed_b = lay_item(ctx);
lay_id filler = lay_item(ctx);
lay_set_contain(ctx, root, LAY_COLUMN);
lay_set_size_xy(ctx, root, 50, 60);
lay_set_size_xy(ctx, fixed_a, 50, 15);
lay_set_size_xy(ctx, fixed_b, 50, 15);
lay_set_behave(ctx, filler, LAY_FILL);
lay_insert(ctx, root, fixed_a);
lay_insert(ctx, root, filler);
lay_insert(ctx, root, fixed_b);
lay_run_context(ctx);
LTEST_VEC4EQ(lay_get_rect(ctx, root), 0, 0, 50, 60);
LTEST_VEC4EQ(lay_get_rect(ctx, fixed_a), 0, 0, 50, 15);
LTEST_VEC4EQ(lay_get_rect(ctx, filler), 0, 15, 50, 30);
LTEST_VEC4EQ(lay_get_rect(ctx, fixed_b), 0, 45, 50, 15);
}
LTEST_DECLARE(simple_margins_1)
{
lay_id root = lay_item(ctx);
lay_id child_a = lay_item(ctx);
lay_id child_b = lay_item(ctx);
lay_id child_c = lay_item(ctx);
lay_set_contain(ctx, root, LAY_COLUMN);
lay_set_behave(ctx, child_a, LAY_HFILL);
lay_set_behave(ctx, child_b, LAY_FILL);
lay_set_behave(ctx, child_c, LAY_HFILL);
lay_set_size_xy(ctx, root, 100, 90);
lay_set_margins_ltrb(ctx, child_a, 3, 5, 7, 10);
lay_set_size_xy(ctx, child_a, 0, (30 - (5 + 10)));
lay_set_size_xy(ctx, child_c, 0, 30);
lay_insert(ctx, root, child_a);
lay_insert(ctx, root, child_b);
lay_insert(ctx, root, child_c);
lay_run_context(ctx);
// Querying for the margins we set should give us the same value, and the
// _ltrb version should also be the same.
lay_vec4 child_a_margins = lay_get_margins(ctx, child_a);
lay_scalar child_a_margins_cs[4];
lay_get_margins_ltrb(ctx, child_a, &child_a_margins_cs[0], &child_a_margins_cs[1], &child_a_margins_cs[2], &child_a_margins_cs[3]);
LTEST_TRUE(child_a_margins[0] == 3 && child_a_margins[1] == 5 && child_a_margins[2] == 7 && child_a_margins[3] == 10);
LTEST_TRUE(child_a_margins[0] == child_a_margins_cs[0] && child_a_margins[1] == child_a_margins_cs[1] && child_a_margins[2] == child_a_margins_cs[2] && child_a_margins[3] == child_a_margins_cs[3]);
// The resulting calculated rects should match these values.
LTEST_VEC4EQ(lay_get_rect(ctx, child_a), 3, 5, 90, (5 + 10));
LTEST_VEC4EQ(lay_get_rect(ctx, child_b), 0, 30, 100, 30);
LTEST_VEC4EQ(lay_get_rect(ctx, child_c), 0, 60, 100, 30);
}
LTEST_DECLARE(nested_boxes_1)
{
const size_t num_rows = 5;
// one of the rows is "fake" and will have 0 units tall height
const size_t num_rows_with_height = num_rows - 1;
lay_id root = lay_item(ctx);
// main_child is a column that contains rows, and those rows
// will contain columns.
lay_id main_child = lay_item(ctx);
lay_set_size_xy(ctx, root,
70,
// 10 units extra size above and below for main_child
// margin
(num_rows_with_height * 10 + 2 * 10)
);
lay_set_margins_ltrb(ctx, main_child, 10, 10, 10, 10);
lay_set_contain(ctx, main_child, LAY_COLUMN);
lay_insert(ctx, root, main_child);
lay_set_behave(ctx, main_child, LAY_FILL);
lay_id *rows = (lay_id*)calloc(num_rows, sizeof(lay_id));
// auto-filling columns-in-row, each one should end up being
// 10 units wide
rows[0] = lay_item(ctx);
lay_set_contain(ctx, rows[0], LAY_ROW);
lay_set_behave(ctx, rows[0], LAY_FILL);
lay_id cols1[5];
// hmm so both the row and its child columns need to be set to
// fill? which means main_child also needs to be set to fill?
for (uint16_t i = 0; i < 5; ++i) {
lay_id col = lay_item(ctx);
// fill empty space
lay_set_behave(ctx, col, LAY_FILL);
lay_insert(ctx, rows[0], col);
cols1[i] = col;
}
rows[1] = lay_item(ctx);
lay_set_contain(ctx, rows[1], LAY_ROW);
lay_set_behave(ctx, rows[1], LAY_VFILL);
lay_id cols2[5];
for (uint16_t i = 0; i < 5; ++i) {
lay_id col = lay_item(ctx);
// fixed-size horizontally, fill vertically
lay_set_size_xy(ctx, col, 10, 0);
lay_set_behave(ctx, col, LAY_VFILL);
lay_insert(ctx, rows[1], col);
cols2[i] = col;
}
// these columns have an inner item which sizes them
rows[2] = lay_item(ctx);
lay_set_contain(ctx, rows[2], LAY_ROW);
lay_id cols3[2];
for (uint16_t i = 0; i < 2; ++i) {
lay_id col = lay_item(ctx);
lay_id inner_sizer = lay_item(ctx);
// only the second one will have height
lay_set_size_xy(ctx, inner_sizer, 25, (lay_scalar)(10 * i));
// align to bottom, only should make a difference for
// first item
lay_set_behave(ctx, col, LAY_BOTTOM);
lay_insert(ctx, col, inner_sizer);
lay_insert(ctx, rows[2], col);
cols3[i] = col;
}
// row 4 should end up being 0 units tall after layout
rows[3] = lay_item(ctx);
lay_set_contain(ctx, rows[3], LAY_ROW);
lay_set_behave(ctx, rows[3], LAY_HFILL);
lay_id cols4[99];
for (uint16_t i = 0; i < 99; ++i) {
lay_id col = lay_item(ctx);
lay_insert(ctx, rows[3], col);
cols4[i] = col;
}
// row 5 should be 10 pixels tall after layout, and each of
// its columns should be 1 pixel wide
rows[4] = lay_item(ctx);
lay_set_contain(ctx, rows[4], LAY_ROW);
lay_set_behave(ctx, rows[4], LAY_FILL);
lay_id cols5[50];
for (uint16_t i = 0; i < 50; ++i) {
lay_id col = lay_item(ctx);
lay_set_behave(ctx, col, LAY_FILL);
lay_insert(ctx, rows[4], col);
cols5[i] = col;
}
for (uint16_t i = 0; i < num_rows; ++i) {
lay_insert(ctx, main_child, rows[i]);
}
// Repeat the run and tests multiple times to make sure we get the expected
// results each time. The original version of oui would overwrite its input
// state (intentionally) with the output state, so the context's input data
// (margins, size) had to be "rebuilt" by the client code by doing a reset
// and then filling it back up for each run. 'lay' does not have that
// restriction.
//
// This is one of the more complex tests, so it's a good
// choice for testing multiple runs of the same context.
for (uint32_t run_n = 0; run_n < 5; ++run_n) {
printf(" Iteration #%d\n", run_n + 1);
lay_run_context(ctx);
LTEST_VEC4EQ(lay_get_rect(ctx, main_child), 10, 10, 50, 40);
// These rows should all be 10 units in height
LTEST_VEC4EQ(lay_get_rect(ctx, rows[0]), 10, 10, 50, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, rows[1]), 10, 20, 50, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, rows[2]), 10, 30, 50, 10);
// this row should have 0 height
LTEST_VEC4EQ(lay_get_rect(ctx, rows[3]), 10, 40, 50, 0);
LTEST_VEC4EQ(lay_get_rect(ctx, rows[4]), 10, 40, 50, 10);
for (int16_t i = 0; i < 5; ++i) {
lay_vec4 r = lay_get_rect(ctx, cols1[i]);
// each of these should be 10 units wide, and stacked
// horizontally
LTEST_VEC4EQ(r, 10 + 10 * i, 10, 10, 10);
}
// the cols in the second row are similar to first row
for (int16_t i = 0; i < 5; ++i) {
lay_vec4 r = lay_get_rect(ctx, cols2[i]);
LTEST_VEC4EQ(r, 10 + 10 * i, 20, 10, 10);
}
// leftmost (first of two items), aligned to bottom of row, 0
// units tall
LTEST_VEC4EQ(lay_get_rect(ctx, cols3[0]), 10, 40, 25, 0);
// rightmost (second of two items), same height as row, which
// is 10 units tall
LTEST_VEC4EQ(lay_get_rect(ctx, cols3[1]), 35, 30, 25, 10);
// these should all have size 0 and be in the middle of the
// row
for (int16_t i = 0; i < 99; ++i) {
lay_vec4 r = lay_get_rect(ctx, cols4[i]);
LTEST_VEC4EQ(r, 25 + 10, 40, 0, 0);
}
// these should all be 1 unit wide and 10 units tall
for (int16_t i = 0; i < 50; ++i) {
lay_vec4 r = lay_get_rect(ctx, cols5[i]);
LTEST_VEC4EQ(r, 10 + i, 40, 1, 10);
}
}
free(rows);
}
LTEST_DECLARE(deep_nest_1)
{
lay_id root = lay_item(ctx);
const int16_t num_items = 500;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
lay_id parent = root;
for (int16_t i = 0; i < num_items; ++i)
{
lay_id item = lay_item(ctx);
lay_insert(ctx, parent, item);
parent = item;
}
lay_set_size_xy(ctx, parent, 77, 99);
lay_run_context(ctx);
LTEST_VEC4EQ(lay_get_rect(ctx, root), 0, 0, 77, 99)
free(items);
}
LTEST_DECLARE(many_children_1)
{
const int16_t num_items = 20000;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 1, 0);
lay_set_contain(ctx, root, LAY_COLUMN);
lay_id prev = lay_item(ctx);
lay_set_size_xy(ctx, prev, 1, 1);
lay_insert(ctx, root, prev);
for (int16_t i = 0; i < num_items - 1; ++i)
{
lay_id item = lay_item(ctx);
lay_set_size_xy(ctx, item, 1, 1);
lay_append(ctx, prev, item);
prev = item;
}
lay_run_context(ctx);
// with each child item being 1 unit tall, the total height should be num_items
LTEST_VEC4EQ(lay_get_rect(ctx, root), 0, 0, 1, num_items)
free(items);
}
LTEST_DECLARE(child_align_1)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 50, 50);
#define MK_ALIGN_BOX(n, flags) \
lay_id aligned_box_##n = lay_item(ctx); \
lay_set_size_xy(ctx, aligned_box_##n, 10, 10); \
lay_set_behave(ctx, aligned_box_##n, flags); \
lay_insert(ctx, root, aligned_box_##n);
MK_ALIGN_BOX(1, LAY_TOP | LAY_LEFT);
MK_ALIGN_BOX(2, LAY_TOP | LAY_RIGHT);
MK_ALIGN_BOX(3, LAY_TOP | LAY_HCENTER);
MK_ALIGN_BOX(4, LAY_VCENTER | LAY_LEFT);
MK_ALIGN_BOX(5, LAY_VCENTER | LAY_RIGHT);
MK_ALIGN_BOX(6, LAY_VCENTER | LAY_HCENTER);
MK_ALIGN_BOX(7, LAY_BOTTOM | LAY_LEFT);
MK_ALIGN_BOX(8, LAY_BOTTOM | LAY_RIGHT);
MK_ALIGN_BOX(9, LAY_BOTTOM | LAY_HCENTER);
#undef MK_ALIGN_BOX
lay_run_context(ctx);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_1), 0, 0, 10, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_2), 40, 0, 10, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_3), 20, 0, 10, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_4), 0, 20, 10, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_5), 40, 20, 10, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_6), 20, 20, 10, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_7), 0, 40, 10, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_8), 40, 40, 10, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_9), 20, 40, 10, 10);
}
LTEST_DECLARE(child_align_2)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 50, 50);
#define MK_ALIGN_BOX(n, flags) \
lay_id aligned_box_##n = lay_item(ctx); \
lay_set_size_xy(ctx, aligned_box_##n, 10, 10); \
lay_set_behave(ctx, aligned_box_##n, flags); \
lay_insert(ctx, root, aligned_box_##n);
MK_ALIGN_BOX(1, LAY_TOP | LAY_HFILL);
MK_ALIGN_BOX(2, LAY_VCENTER | LAY_HFILL);
MK_ALIGN_BOX(3, LAY_BOTTOM | LAY_HFILL);
MK_ALIGN_BOX(4, LAY_VFILL | LAY_LEFT);
MK_ALIGN_BOX(5, LAY_VFILL | LAY_RIGHT);
MK_ALIGN_BOX(6, LAY_VFILL | LAY_HCENTER);
#undef MK_ALIGN_BOX
lay_run_context(ctx);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_1), 0, 0, 50, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_2), 0, 20, 50, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_3), 0, 40, 50, 10);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_4), 0, 0, 10, 50);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_5), 40, 0, 10, 50);
LTEST_VEC4EQ(lay_get_rect(ctx, aligned_box_6), 20, 0, 10, 50);
}
LTEST_DECLARE(wrap_row_1)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 50, 50);
lay_set_contain(ctx, root, LAY_ROW | LAY_WRAP);
// We will create a 5x5 grid of boxes that are 10x10 units per each box.
// There should be no empty space, gaps, or extra wrapping.
const int16_t num_items = 5 * 5;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
for (int16_t i = 0; i < num_items; ++i) {
lay_id item = lay_item(ctx);
lay_set_size_xy(ctx, item, 10, 10);
lay_insert(ctx, root, item);
items[i] = item;
}
lay_run_context(ctx);
for (int16_t i = 0; i < num_items; ++i) {
int16_t x, y;
x = i % 5;
y = i / 5;
LTEST_VEC4EQ(lay_get_rect(ctx, items[i]), x * 10, y * 10, 10, 10);
}
free(items);
}
LTEST_DECLARE(wrap_row_2)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 57, 57);
lay_set_contain(ctx, root, LAY_ROW | LAY_WRAP | LAY_START);
// This one should have extra space on the right edge and bottom (7 units)
const int16_t num_items = 5 * 5;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
for (int16_t i = 0; i < num_items; ++i) {
lay_id item = lay_item(ctx);
lay_set_size_xy(ctx, item, 10, 10);
lay_insert(ctx, root, item);
items[i] = item;
}
lay_run_context(ctx);
for (int16_t i = 0; i < num_items; ++i) {
int16_t x, y;
x = i % 5;
y = i / 5;
LTEST_VEC4EQ(lay_get_rect(ctx, items[i]), x * 10, y * 10, 10, 10);
}
free(items);
}
LTEST_DECLARE(wrap_row_3)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 57, 57);
lay_set_contain(ctx, root, LAY_ROW | LAY_WRAP | LAY_END);
// This one should have extra space on the left edge and bottom (7 units)
const int16_t num_items = 5 * 5;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
for (int16_t i = 0; i < num_items; ++i) {
lay_id item = lay_item(ctx);
lay_set_size_xy(ctx, item, 10, 10);
//lay_set_behave(ctx, item, LAY_BOTTOM | LAY_RIGHT);
lay_insert(ctx, root, item);
items[i] = item;
}
lay_run_context(ctx);
for (int16_t i = 0; i < num_items; ++i) {
int16_t x, y;
x = i % 5;
y = i / 5;
//print_vec4(lay_get_rect(ctx, items[i]));
//printf("\n");
LTEST_VEC4EQ(lay_get_rect(ctx, items[i]), 7 + x * 10, y * 10, 10, 10);
}
free(items);
}
LTEST_DECLARE(wrap_row_4)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 58, 57);
lay_set_contain(ctx, root, LAY_ROW | LAY_WRAP | LAY_MIDDLE);
lay_id spacer = lay_item(ctx);
lay_set_size_xy(ctx, spacer, 58, 7);
lay_insert(ctx, root, spacer);
// This one should split the horizontal extra space between the left and
// right, and have the vertical extra space at the top (via extra inserted
// spacer item, with explicit size)
const int16_t num_items = 5 * 5;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
for (int16_t i = 0; i < num_items; ++i) {
lay_id item = lay_item(ctx);
lay_set_size_xy(ctx, item, 10, 10);
lay_insert(ctx, root, item);
items[i] = item;
}
lay_run_context(ctx);
for (int16_t i = 0; i < num_items; ++i) {
int16_t x, y;
x = i % 5;
y = i / 5;
LTEST_VEC4EQ(lay_get_rect(ctx, items[i]), 4 + x * 10, 7 + y * 10, 10, 10);
}
free(items);
}
// bug? the last row of a LAY_JUSTIFY wrapping row container will not have its
// space divided evenly, and will instead behave like LAY_MIDDLE (or LAY_START?)
LTEST_DECLARE(wrap_row_5)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 54, 50);
lay_set_contain(ctx, root, LAY_ROW | LAY_WRAP | LAY_JUSTIFY);
const int16_t num_items = 5 * 5;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
for (int16_t i = 0; i < num_items; ++i) {
lay_id item = lay_item(ctx);
lay_set_size_xy(ctx, item, 10, 10);
lay_insert(ctx, root, item);
items[i] = item;
}
lay_run_context(ctx);
// TODO note we're adding the -5 here so we ignore the last row, which
// seems to be bugged
for (int16_t i = 0; i < num_items - 5; ++i) {
int16_t x, y;
x = i % 5;
y = i / 5;
LTEST_VEC4EQ(lay_get_rect(ctx, items[i]), x * 11, y * 10, 10, 10);
}
free(items);
}
// Same as wrap_row_1, but for columns
LTEST_DECLARE(wrap_column_1)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 50, 50);
lay_set_contain(ctx, root, LAY_COLUMN | LAY_WRAP);
// We will create a 5x5 grid of boxes that are 10x10 units per each box.
// There should be no empty space, gaps, or extra wrapping.
const int16_t num_items = 5 * 5;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
for (int16_t i = 0; i < num_items; ++i) {
lay_id item = lay_item(ctx);
lay_set_size_xy(ctx, item, 10, 10);
lay_insert(ctx, root, item);
items[i] = item;
}
lay_run_context(ctx);
for (int16_t i = 0; i < num_items; ++i) {
int16_t x, y;
y = i % 5;
x = i / 5;
LTEST_VEC4EQ(lay_get_rect(ctx, items[i]), x * 10, y * 10, 10, 10);
}
free(items);
}
LTEST_DECLARE(wrap_column_2)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 57, 57);
lay_set_contain(ctx, root, LAY_COLUMN | LAY_WRAP | LAY_START);
// This one should have extra space on the right and bottom (7 units)
const int16_t num_items = 5 * 5;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
for (int16_t i = 0; i < num_items; ++i) {
lay_id item = lay_item(ctx);
lay_set_size_xy(ctx, item, 10, 10);
lay_insert(ctx, root, item);
items[i] = item;
}
lay_run_context(ctx);
for (int16_t i = 0; i < num_items; ++i) {
int16_t x, y;
y = i % 5;
x = i / 5;
LTEST_VEC4EQ(lay_get_rect(ctx, items[i]), x * 10, y * 10, 10, 10);
}
free(items);
}
LTEST_DECLARE(wrap_column_3)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 57, 57);
lay_set_contain(ctx, root, LAY_COLUMN | LAY_WRAP | LAY_END);
// This one should have extra space on the top and right (7 units)
const int16_t num_items = 5 * 5;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
for (int16_t i = 0; i < num_items; ++i) {
lay_id item = lay_item(ctx);
lay_set_size_xy(ctx, item, 10, 10);
//lay_set_behave(ctx, item, LAY_BOTTOM | LAY_RIGHT);
lay_insert(ctx, root, item);
items[i] = item;
}
lay_run_context(ctx);
for (int16_t i = 0; i < num_items; ++i) {
int16_t x, y;
y = i % 5;
x = i / 5;
//print_vec4(lay_get_rect(ctx, items[i]));
//printf("\n");
LTEST_VEC4EQ(lay_get_rect(ctx, items[i]), x * 10, 7 + y * 10, 10, 10);
}
free(items);
}
// Just like wrap_row_4, but as columns instead of rows
LTEST_DECLARE(wrap_column_4)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 57, 58);
lay_set_contain(ctx, root, LAY_COLUMN | LAY_WRAP | LAY_MIDDLE);
lay_id spacer = lay_item(ctx);
lay_set_size_xy(ctx, spacer, 7, 58);
lay_insert(ctx, root, spacer);
const int16_t num_items = 5 * 5;
lay_id *items = (lay_id*)calloc(num_items, sizeof(lay_id));
for (int16_t i = 0; i < num_items; ++i) {
lay_id item = lay_item(ctx);
lay_set_size_xy(ctx, item, 10, 10);
lay_insert(ctx, root, item);
items[i] = item;
}
lay_run_context(ctx);
for (int16_t i = 0; i < num_items; ++i) {
int16_t x, y;
y = i % 5;
x = i / 5;
LTEST_VEC4EQ(lay_get_rect(ctx, items[i]), 7 + x * 10, 4 + y * 10, 10, 10);
}
free(items);
}
LTEST_DECLARE(anchor_right_margin1)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 100, 100);
lay_id child = lay_item(ctx);
lay_set_size_xy(ctx, child, 50, 50);
lay_set_margins_ltrb(ctx, child, 5, 5, 0, 0);
lay_set_behave(ctx, child, LAY_BOTTOM | LAY_RIGHT);
lay_insert(ctx, root, child);
lay_run_context(ctx);
LTEST_VEC4EQ(lay_get_rect(ctx, child), 50, 50, 50, 50);
}
LTEST_DECLARE(anchor_right_margin2)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 100, 100);
lay_id child = lay_item(ctx);
lay_set_size_xy(ctx, child, 50, 50);
lay_set_margins_ltrb(ctx, child, 5, 5, 10, 10);
lay_set_behave(ctx, child, LAY_BOTTOM | LAY_RIGHT);
lay_insert(ctx, root, child);
lay_run_context(ctx);
LTEST_VEC4EQ(lay_get_rect(ctx, child), 40, 40, 50, 50);
}
// Call in main to run a test by name
//
// Resets string buffer and lay context before running test
#define LTEST_RUN(testname) \
lay_reset_context(&ctx); \
printf(" * " #testname "\n"); \
test_##testname(&ctx);
int main(int argc, char** argv)
{
(void)argc;
(void)argv;
#ifdef _WIN32
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
SetUnhandledExceptionFilter(LayTestUnhandledExceptionFilter);
#endif
lay_context ctx;
lay_init_context(&ctx);
printf("Running tests\n");
LTEST_RUN(testing_sanity);
LTEST_RUN(simple_fill);
LTEST_RUN(reserve_capacity);
LTEST_RUN(multiple_uninserted);
LTEST_RUN(column_even_fill);
LTEST_RUN(row_even_fill);
LTEST_RUN(fixed_and_fill);
LTEST_RUN(simple_margins_1);
LTEST_RUN(nested_boxes_1);
LTEST_RUN(deep_nest_1);
LTEST_RUN(many_children_1);
LTEST_RUN(child_align_1);
LTEST_RUN(child_align_2);
LTEST_RUN(wrap_row_1);
LTEST_RUN(wrap_row_2);
LTEST_RUN(wrap_row_3);
LTEST_RUN(wrap_row_4);
LTEST_RUN(wrap_row_5);
LTEST_RUN(wrap_column_1);
LTEST_RUN(wrap_column_2);
LTEST_RUN(wrap_column_3);
LTEST_RUN(wrap_column_4);
LTEST_RUN(anchor_right_margin1);
LTEST_RUN(anchor_right_margin2);
printf("Finished tests\n");
lay_destroy_context(&ctx);
return 0;
}