forked from Zuzu-Typ/PyGLM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyGLM_test.py
2521 lines (1996 loc) · 94.7 KB
/
PyGLM_test.py
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
import glm, sys, random, time, copy, re, math
from collections import OrderedDict
glm.silence(0)
## type checking definitions ##
PyGLM_DT_UNKNOWN = 0x0000000
PyGLM_DT_FLOAT = 0x0000001
PyGLM_DT_DOUBLE = 0x0000002
PyGLM_DT_INT = 0x0000004
PyGLM_DT_UINT = 0x0000008
PyGLM_DT_INT8 = 0x0000010
PyGLM_DT_UINT8 = 0x0000020
PyGLM_DT_INT16 = 0x0000040
PyGLM_DT_UINT16 = 0x0000080
PyGLM_DT_INT64 = 0x0000100
PyGLM_DT_UINT64 = 0x0000200
PyGLM_DT_BOOL = 0x0000400
PyGLM_SHAPE_2x2 = 0x0000800
PyGLM_SHAPE_2x3 = 0x0001000
PyGLM_SHAPE_2x4 = 0x0002000
PyGLM_SHAPE_3x2 = 0x0004000
PyGLM_SHAPE_3x3 = 0x0008000
PyGLM_SHAPE_3x4 = 0x0010000
PyGLM_SHAPE_4x2 = 0x0020000
PyGLM_SHAPE_4x3 = 0x0040000
PyGLM_SHAPE_4x4 = 0x0080000
PyGLM_SHAPE_1 = 0x0100000
PyGLM_SHAPE_2 = 0x0200000
PyGLM_SHAPE_3 = 0x0400000
PyGLM_SHAPE_4 = 0x0800000
PyGLM_T_VEC = 0x1000000
PyGLM_T_MVEC = 0x2000000
PyGLM_T_MAT = 0x4000000
PyGLM_T_QUA = 0x8000000
PyGLM_T_NUMBER = 0x10000000
PyGLM_T_ANY_VEC = (PyGLM_T_VEC | PyGLM_T_MVEC)
PyGLM_T_ANY_ARR = (PyGLM_T_ANY_VEC | PyGLM_T_QUA)
PyGLM_SHAPE_SQUARE = (PyGLM_SHAPE_2x2 | PyGLM_SHAPE_3x3 | PyGLM_SHAPE_4x4)
PyGLM_SHAPE_2xM = (PyGLM_SHAPE_2x2 | PyGLM_SHAPE_2x3 | PyGLM_SHAPE_2x4)
PyGLM_SHAPE_3xM = (PyGLM_SHAPE_3x2 | PyGLM_SHAPE_3x3 | PyGLM_SHAPE_3x4)
PyGLM_SHAPE_4xM = (PyGLM_SHAPE_4x2 | PyGLM_SHAPE_4x3 | PyGLM_SHAPE_4x4)
PyGLM_DT_ALL = ((1 << 11) - 1)
PyGLM_SHAPE_ALL = (((1 << 13) - 1) << 11)
PyGLM_T_ALL = (((1 << 5) - 1) << 24)
PyGLM_ALL = (PyGLM_DT_ALL | PyGLM_SHAPE_ALL | PyGLM_T_ALL)
##/type checking definitions ##
#def get_info(num):
# glob = globals()
# vars_ = [x for x in glob if x.startswith("PyGLM_") and not "ANY" in x and not "xM" in x and not "ALL" in x and not "SQUARE" in x]
# out = []
# for var in vars_:
# if (glob[var] & num):
# out.append(var)
# return ", ".join(out)
#def get_info_of(obj):
# return get_info(glm._get_type_info(PyGLM_ALL, obj))
#gio = get_info_of
datatypes = ("float", "double", "int", "glm::uint", "glm::i64", "glm::u64", "glm::i16", "glm::u16", "glm::i8", "glm::u8", "bool")
prefixes = ("f", "d", "i", "u", "i64", "u64", "i16", "u16", "i8", "u8", "b")
suffixes = "fFiIqQsSuUB"
vector_type_ids = tuple(range(len(datatypes)))
matrix_type_ids = tuple(range(4))
quat_type_ids = tuple(range(2))
vector_type_dict = {}
vector_length_dict = {}
vector_types = []
matrix_type_dict = {}
matrix_length_dict = {}
matrix_types = []
quat_type_dict = {}
quat_types = []
ctypes_types = [glm.c_float, glm.c_double, glm.c_int64, glm.c_int32, glm.c_int16, glm.c_int8, glm.c_uint64, glm.c_uint32, glm.c_uint16, glm.c_uint8, glm.c_bool]
for type_id in vector_type_ids:
vector_type_dict[type_id] = []
this_list = vector_type_dict[type_id]
for L in range(1, 5):
tp = getattr(glm, "{T}vec{L}".format(T=prefixes[type_id], L=L))
vll = vector_length_dict.get(L, [])
vll.append(tp)
vector_length_dict[L] = vll
this_list.append(tp)
vector_types += this_list
for type_id in matrix_type_ids:
matrix_type_dict[type_id] = []
this_list = matrix_type_dict[type_id]
for C in range(2, 5):
for R in range(2, 5):
tp = getattr(glm, "{T}mat{C}x{R}".format(T=prefixes[type_id], C=C, R=R))
mll = matrix_length_dict.get((C,R), [])
mll.append(tp)
matrix_length_dict[(C,R)] = mll
this_list.append(tp)
matrix_types += this_list
for type_id in quat_type_ids:
quat_type_dict[type_id] = []
this_list = quat_type_dict[type_id]
this_list.append(getattr(glm, "{T}quat".format(T=prefixes[type_id])))
quat_types += this_list
obj_gen = lambda types: (T() for T in types)
def list_replace(list_, x, y):
list_copy = list(list_)
for i in range(len(list_copy)):
list_copy[i] = list_copy[i].replace(x, y)
return list_copy
randf = lambda: random.random()*100
randfs = lambda: randf()-50
mvec_mats = {
"f2" : glm.fmat2x2(0),
"f3" : glm.fmat2x3(0),
"f4" : glm.fmat2x4(0),
"d2" : glm.dmat2x2(0),
"d3" : glm.dmat2x3(0),
"d4" : glm.dmat2x4(0),
"i2" : glm.imat2x2(0),
"i3" : glm.imat2x3(0),
"i4" : glm.imat2x4(0),
"u2" : glm.umat2x2(0),
"u3" : glm.umat2x3(0),
"u4" : glm.umat2x4(0),
}
def get_args(arg_string, type_, rand_func=None):
if not rand_func:
if type_ in "IQSU":
rand_func = randf
else:
rand_func = randfs
args = []
for arg in arg_string:
if arg == "N":
if type_ in "fF":
args.append(float(rand_func()))
if type_ in "iqsu":
args.append(int(rand_func()))
if type_ in "IQSU":
args.append(int(randf()))
if type_ == "B":
args.append(random.choice((True, False)))
elif arg == "Ni":
args.append(int(rand_func()))
elif arg == "NB":
args.append(random.choice((True, False)))
elif "V" in arg:
if len(arg) == 3 and arg[2] in "fFiB":
T = prefixes[suffixes.index(arg[2])]
else:
T = prefixes[suffixes.index(type_)]
L = int(arg[1])
rf = rand_func
if T == "b":
rf = lambda: random.choice((True, False))
args.append(getattr(glm, "{T}vec{L}".format(T=T, L=L))(*(rf() for x in range(L))))
elif "P" in arg:
if len(arg) == 3 and arg[2] in "fFi":
T = prefixes[suffixes.index(arg[2])]
else:
T = prefixes[suffixes.index(type_)]
L = int(arg[1])
args.append(mvec_mats["{T}{L}".format(T=T, L=L)][0])
elif arg == "Q":
args.append(getattr(glm, "{T}quat".format(T=prefixes[suffixes.index(type_)]))(rand_func(), rand_func(), rand_func(), rand_func()))
elif "M" in arg:
if len(arg) == 4 and arg[3] in "fFiI":
T = prefixes[suffixes.index(arg[3])]
else:
T = prefixes[suffixes.index(type_)]
n = int(arg[1])
m = int(arg[2])
args.append(getattr(glm, "{T}mat{n}x{m}".format(T=prefixes[suffixes.index(type_)], n=n, m=m))(*[rand_func() for i in range(n*m)]))
return args
def gen_args(args_string):
if "#M" in args_string:
supports_mvec = True
args_string = args_string.replace("#M", "")
else:
supports_mvec = False
if "#u" in args_string:
rand_func = randf
args_string = args_string.replace("#u", "")
elif "#p" in args_string:
rand_func = lambda: randf() + 1
args_string = args_string.replace("#p", "")
elif "#d" in args_string:
rand_func = random.random
args_string = args_string.replace("#d", "")
elif "#x" in args_string:
res = randf()
rand_func = lambda: res
args_string = args_string.replace("#x", "")
else:
rand_func = None
if "__" in args_string:
arg_part, type_part = args_string.split("__")
parts = arg_part.split("_")
types = type_part
else:
parts = args_string.split("_")
types = suffixes
mvec_types = ("f" if "f" in types else "") + ("F" if "F" in types else "") + ("i" if "i" in types else "") + ("I" if "I" in types else "")
for part in parts:
arg_strings = []
current_text = ""
for char in part:
if not char in "1234" + "fFiB":
if current_text != "":
arg_strings.append(current_text)
current_text = ""
current_text += char
if current_text:
arg_strings.append(current_text)
if "N" in arg_strings and not ("V" in arg_strings or "M" in arg_strings):
yield get_args(arg_strings, "F" if "F" in types else "f" if "f" in types else "i" if "i" in types else "I" if "I" in types else types[0], rand_func)
elif "V" in arg_strings:
for T in types:
for L in range(1, 5):
yield get_args(list_replace(arg_strings, "V", "V{L}".format(L=L)), T, rand_func)
if supports_mvec:
for T in mvec_types:
for L in range(2, 5):
yield get_args(list_replace(arg_strings, "V", "P{L}".format(L=L)), T, rand_func)
elif "P" in arg_strings:
for T in mvec_types:
for L in range(2, 5):
yield get_args(list_replace(arg_strings, "P", "P{L}".format(L=L)), T, rand_func)
elif "Vf" in arg_strings:
for L in range(1, 5):
yield get_args(list_replace(arg_strings, "Vf", "V{L}".format(L=L)), "f", rand_func)
if supports_mvec:
for L in range(2, 5):
yield get_args(list_replace(arg_strings, "Vf", "P{L}".format(L=L)), "f", rand_func)
elif "VF" in arg_strings:
for L in range(1, 5):
yield get_args(list_replace(arg_strings, "VF", "V{L}".format(L=L)), "F", rand_func)
if supports_mvec:
for L in range(2, 5):
yield get_args(list_replace(arg_strings, "VF", "P{L}".format(L=L)), "F", rand_func)
elif "Vi" in arg_strings:
for L in range(1, 5):
yield get_args(list_replace(arg_strings, "Vi", "V{L}".format(L=L)), "i", rand_func)
if supports_mvec:
for L in range(2, 5):
yield get_args(list_replace(arg_strings, "Vi", "P{L}".format(L=L)), "i", rand_func)
elif "VB" in arg_strings:
for L in range(1, 5):
yield get_args(list_replace(arg_strings, "VB", "V{L}".format(L=L)), "B", lambda: random.choice((True, False)))
elif "V1" in arg_strings or "V2" in arg_strings or "V3" in arg_strings or "V4" in arg_strings or "M22" in arg_strings or "M23" in arg_strings or "M24" in arg_strings or "M32" in arg_strings or "M33" in arg_strings or "M34" in arg_strings or "M42" in arg_strings or "M43" in arg_strings or "M44" in arg_strings:
for T in types:
yield get_args(arg_strings, T, rand_func)
if supports_mvec:
for T in mvec_types:
yield get_args(list_replace(arg_strings, "V", "P"), T, rand_func)
elif "Q" in arg_strings:
for T in types:
if not T in "fF":
continue
yield get_args(arg_strings, T, rand_func)
elif "M" in arg_strings:
for T in types:
if not T in "fFiI":
continue
for C in range(2,5):
for R in range(2,5):
yield get_args(list_replace(arg_strings, "M", "M{C}{R}".format(C=C,R=R)), T, rand_func)
elif "Mf" in arg_strings:
for C in range(2,5):
for R in range(2,5):
yield get_args(list_replace(arg_strings, "Mf", "M{C}{R}".format(C=C,R=R)), "f", rand_func)
elif "MF" in arg_strings:
for C in range(2,5):
for R in range(2,5):
yield get_args(list_replace(arg_strings, "MF", "M{C}{R}".format(C=C,R=R)), "F", rand_func)
elif "-" in arg_strings:
yield ()
def gen_obj(args_string):
for args in gen_args(args_string):
yield args[0]
#v1 = glm.vec1()
#v2 = glm.vec2()
#v3 = glm.vec3()
#v4 = glm.vec4()
#vectors = [v1, v2, v3, v4]
#vector_types = [glm.vec1, glm.vec2, glm.vec3, glm.vec4]
#m22 = glm.mat2x2()
#m23 = glm.mat2x3()
#m24 = glm.mat2x4()
#m32 = glm.mat3x2()
#m33 = glm.mat3x3()
#m34 = glm.mat3x4()
#m42 = glm.mat4x2()
#m43 = glm.mat4x3()
#m44 = glm.mat4x4()
#matrices = [m22, m23, m24, m32, m33, m34, m42, m43, m44]
#matrix_types = [glm.mat2x2, glm.mat2x3, glm.mat2x4, glm.mat3x2, glm.mat3x3, glm.mat3x4, glm.mat4x2, glm.mat4x3, glm.mat4x4]
#q = glm.quat()
#all_types = vector_types + matrix_types + [glm.quat]
#all_type_objects = vectors + matrices + [q]
#get_obj_generator = lambda types: (x() for x in types)
class FAssertionError(Exception):
pass
def fassert(func, args):
try:
return func(*args)
except:
raise FAssertionError("{} raised {} with {}".format(func, sys.exc_info()[1], repr(args)))
def fail(*args):
raise FAssertionError("Failed with " + str(args))
# Specific #
def test_specific():
assert isinstance(glm.version, str)
assert isinstance(glm.license, str)
#/Specific #
# Initialization #
## vec1
def test_vec1_types():
for args in gen_args("#u-_N_V1"): # need to add support for _V1_V2_V3_V4
for T in vector_length_dict[1]:
fassert(T, args)
## vec2
def test_vec2_types():
for args in gen_args("#u-_N_NN_V2"):
for T in vector_length_dict[2]:
fassert(T, args)
for args in gen_args("#uV2V3_V2V4"):
fassert(type(args[0]), args[1:])
## vec3
def test_vec3_types():
for args in gen_args("#u-_N_NNN_V3"):
for T in vector_length_dict[3]:
fassert(T, args)
for args in gen_args("#uV3V4_V3NV2_V3V2N"):
fassert(type(args[0]), args[1:])
## vec4
def test_vec4_types():
for args in gen_args("#u-_N_NNNN_V4"):
for T in vector_length_dict[4]:
fassert(T, args)
for args in gen_args("#uV4V2NN_V4NV2N_V4NNV2_V4NV3_V4V3N"):
fassert(type(args[0]), args[1:])
## mat
def test_mat_types():
for C in range(2, 5):
for R in range(2, 5):
for args in gen_args("#u-_N_" + "N"*(C*R) + "_M{C}{R}__fFiI".format(C=C, R=R)): # need support for _M
for T in matrix_length_dict[(C,R)]:
fassert(T, args)
for args in gen_args("#uM{C}{R}".format(C=C, R=R) + "V{R}".format(R=R)*C + "_" + "_".join(["M{C}{R}M{c}{r}".format(C=C, R=R, c=c, r=r) for c in range(2, 5) for r in range(2,5)]) + "__fFiI"):
fassert(type(args[0]), args[1:])
## quat
def test_quat_types():
for args in gen_args("#u-_V3_M33_M44_NV3_V3V3_NNNN_Q__f"): # need support for conversion constructors
fassert(glm.quat, args)
## dquat
def test_dquat_types():
for args in gen_args("#u-_V3_M33_M44_NV3_V3V3_NNNN_Q__F"): # need support for conversion constructors
fassert(glm.dquat, args)
## array
def test_array_types():
for args in gen_args("V_M_Q_VV_MM_QQ_VVV_MMM_QQQ"):
fassert(glm.array, args)
assert glm.array([glm.vec4() for x in range(10)])
assert glm.array(tuple([glm.vec4() for x in range(10)]))
assert glm.array({x : glm.vec4() for x in range(10)}.values())
assert glm.array(memoryview(glm.array([glm.vec4() for x in range(10)])))
assert glm.array([glm.quat() for x in range(10)])
assert glm.array(tuple([glm.quat() for x in range(10)]))
assert glm.array({x : glm.quat() for x in range(10)}.values())
assert glm.array(memoryview(glm.array([glm.quat() for x in range(10)])))
assert glm.array([glm.mat4() for x in range(10)])
assert glm.array(tuple([glm.mat4() for x in range(10)]))
assert glm.array({x : glm.mat4() for x in range(10)}.values())
assert glm.array(memoryview(glm.array([glm.mat4() for x in range(10)])))
assert glm.array([glm.float32(x) for x in range(10)])
assert glm.array(tuple([glm.float32(x) for x in range(10)]))
assert glm.array({x : glm.float32(x) for x in range(10)}.values())
assert glm.array(memoryview(glm.array([glm.float32(x) for x in range(10)])))
assert glm.array(glm.float32, *range(10))
assert glm.array.from_numbers(glm.float32, *range(10))
assert glm.array([glm.float32, 1, 2, 3]) == glm.array((glm.float32, 1, 2, 3))
assert glm.array(OrderedDict([(glm.float32, 1), (1, 2), (2, 3), (3, 4)]))
assert glm.array(memoryview(glm.array.from_numbers(glm.float32, *range(10))))
assert glm.array.zeros(1000, glm.uint8) == glm.array.zeros(1000, glm.u8vec1) == glm.array(glm.int8, 0).repeat(1000)
assert glm.array.zeros(1000, glm.quat)
assert glm.array.zeros(1000, glm.vec4)
assert glm.array.zeros(1000, glm.mat4)
arr = glm.array(glm.mat4(), glm.mat4(2))
mv = memoryview(arr)
assert glm.array.as_reference(mv) == arr
assert glm.array.as_reference(mv).reference == mv
assert glm.array.as_reference(arr).address == arr.address
assert len(arr) == 2, arr
assert arr.typecode == "f", arr
assert arr.dtype == "float32", arr
assert arr.ctype == glm.float32, arr
assert arr.dt_size == 4, arr
assert arr.itemsize == arr.dt_size * 4 * 4, arr
assert arr.ptr, arr
assert arr.nbytes == arr.itemsize * len(arr), arr
assert arr.element_type == glm.mat4, arr
assert arr.readonly == False
assert arr.reference is None
arr = glm.array.from_numbers(glm.int32, 5, 4, 3, 2, 1)
assert arr.filter(lambda x: True) == arr
assert arr.filter(lambda x: False) == arr.repeat(0)
assert arr.filter(lambda x: x <= 3) == glm.array.from_numbers(glm.int32, 3, 2, 1)
assert arr.map(lambda x: x) == arr
assert arr.map(lambda x: glm.vec3(x)) == glm.array(glm.vec3(5), glm.vec3(4), glm.vec3(3), glm.vec3(2), glm.vec3(1))
assert arr.map(lambda x: (x, x) if x == 1 else None) == glm.array(glm.int32, 1, 1)
assert arr.map(glm.add, glm.array([glm.vec2(x) for x in range(5)])) == glm.array([glm.vec2(5) for x in range(5)])
assert glm.array(glm.int32, 0, 1, 1, 0, 0).map(glm.if_else, arr, glm.array(glm.int32, 1, 2, 3, 4, 5)) == glm.array(glm.int32, 1, 4, 3, 4, 5)
assert glm.array(glm.int32, *(x * 4 for x in range(-5,5))).map(lambda *args: args if sum(args) == 0 else None,
glm.array(glm.int32, *(x * 3 for x in range(-5,5))),
glm.array(glm.int32, *(x * 2 for x in range(-5,5))),
glm.array(glm.int32, *(x * 1 for x in range(-5,5))),
glm.array(glm.int32, *(x * -9.4 for x in range(-5,5)))) == glm.array(glm.int32, 0, 0, 0, 0, 0)
assert arr.map(lambda x: None) == arr.repeat(0)
arr.sort(glm.cmp)
assert arr == glm.array.from_numbers(glm.int32, 1, 2, 3, 4, 5)
arr = glm.array(glm.vec3(5), glm.vec3(4), glm.vec3(3), glm.vec3(2), glm.vec3(1))
assert arr.filter(lambda x: True) == arr
assert arr.filter(lambda x: False) == arr.repeat(0)
assert arr.filter(lambda x: x.x <= 3) == glm.array(glm.vec3(3), glm.vec3(2), glm.vec3(1))
assert arr.map(lambda x: x) == arr
assert arr.map(lambda x: x.xxxx) == glm.array(glm.vec4(5), glm.vec4(4), glm.vec4(3), glm.vec4(2), glm.vec4(1))
assert arr.map(lambda x: (x, x) if x.x == 1 else None) == glm.array(glm.vec3(1), glm.vec3(1))
assert arr.map(lambda x: None) == arr.repeat(0)
assert arr.map(lambda x: (x.x), ctype = glm.int32) == glm.array.from_numbers(glm.int32, 5, 4, 3, 2, 1)
arr.sort(lambda x, y: -(glm.all(glm.lessThan(x, y))))
assert arr == glm.array(glm.vec3(1), glm.vec3(2), glm.vec3(3), glm.vec3(4), glm.vec3(5))
# repr #
def test_repr_eval():
glm_locals = {a : getattr(glm, a) for a in dir(glm)}
for T in vector_types + matrix_types + quat_types:
fassert(lambda o: eval(repr(o), glm_locals), (T(),))
arr = glm.array(glm.vec4(*[-1.23457e+06]*4), glm.vec4(*[-7.65432e+06]*4))
arr2 = glm.array(glm.quat(*[-1.23457e+06]*4), glm.quat(*[-7.65432e+06]*4))
arr3 = glm.array(glm.mat4(*[-1.23457e+06]*16), glm.mat4(*[-7.65432e+06]*16))
assert repr(arr), arr
assert repr(arr2), arr2
assert repr(arr3), arr3
assert str(arr), arr
assert str(arr2), arr2
assert str(arr3), arr3
arr = glm.array(glm.vec4(*[1]*4), glm.vec4(*[2]*4))
arr2 = glm.array(glm.quat(*[1]*4), glm.quat(*[2]*4))
arr3 = glm.array(glm.mat4(*[1]*16), glm.mat4(*[2]*16))
arr4 = glm.array(glm.int8, *range(10))
assert eval(repr(arr), glm_locals) == arr, (arr, repr(arr), eval(repr(arr), glm_locals))
assert eval(repr(arr2), glm_locals) == arr2, (arr2, repr(arr2), eval(repr(arr2), glm_locals))
assert eval(repr(arr3), glm_locals) == arr3, (arr3, repr(arr3), eval(repr(arr3), glm_locals))
assert eval(repr(arr4), glm_locals) == arr4, (arr4, repr(arr4), eval(repr(arr4), glm_locals))
#/repr #
# neg #
def test_neg():
for obj in gen_obj("#MV_M_Q__fFiqsuB"):
fassert(obj.__neg__, ())
assert (-glm.array(obj))[0] == -obj, obj
#/neg #
# pos #
def test_pos():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSU"):
fassert(obj.__pos__, ())
assert (+glm.array(obj))[0] == +obj, obj
#/pos #
# abs #
def test_abs():
for obj in gen_obj("#MV__fFiqsuIQSU"):
fassert(obj.__abs__, ())
assert (abs(glm.array(obj)))[0] == abs(obj), obj
#/abs #
# add #
def test_add():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSUB"):
fassert(obj.__add__, (obj,))
assert (glm.array(obj) + glm.array(obj))[0] == obj + obj, obj
assert (glm.array(obj) + obj)[0] == obj + obj, obj
assert (obj + glm.array(obj))[0] == obj + obj, obj
arr = glm.array(glm.mat4())
fassert(arr.concat, (arr,))
fassert((arr.concat(arr)).concat, (arr,))
#/add #
# sub #
def test_sub():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSU"):
fassert(obj.__sub__, (obj,))
assert (glm.array(obj) - glm.array(obj))[0] == obj - obj, obj
assert (glm.array(obj) - obj)[0] == obj - obj, obj
assert (obj - glm.array(obj))[0] == obj - obj, obj
#/sub #
# mul #
def test_mul():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSUB"):
fassert(obj.__mul__, (1,))
assert (glm.array(obj) * glm.array(glm.array(obj).ctype, 1))[0] == obj * 1, obj
assert (glm.array(obj) * 1)[0] == obj * 1, obj
assert (obj * glm.array(glm.array(obj).ctype, 1))[0] == obj * 1, obj
arr = glm.array(glm.mat4())
fassert(arr.repeat, (3,))
fassert((arr.repeat(4)).repeat, (2,))
#/mul #
# div #
def test_div():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSU"):
fassert(obj.__truediv__, (1,))
for obj in gen_obj("#MV_Q__fFiqsuIQSU"):
fassert(obj.__truediv__, (1,))
assert (glm.array(obj) / glm.array(glm.array(obj).ctype, 1))[0] == obj / 1, obj
assert (glm.array(obj) / 1)[0] == obj / 1, obj
assert (obj / glm.array(glm.array(obj).ctype, 1))[0] == obj / 1, obj
for obj in gen_obj("V__iqsuIQSU"):
try:
obj.__truediv__(type(obj)(0))
fail(obj)
except ZeroDivisionError:
pass
for obj in gen_obj("P__iI"):
try:
obj.__truediv__(obj)
fail(obj)
except ZeroDivisionError:
pass
for obj in gen_obj("#MV_M_Q__iqsuIQSU"):
try:
obj.__truediv__(0)
fail(obj)
except ZeroDivisionError:
pass
#/div #
# mod #
def test_mod():
for obj in gen_obj("#MV__fF"):
fassert(obj.__mod__, (1,))
for obj in gen_obj("#MV__fF"):
assert (glm.array(obj) % glm.array(glm.array(obj).ctype, 1))[0] == obj % 1, obj
assert (glm.array(obj) % 1)[0] == obj % 1, obj
assert (obj % glm.array(glm.array(obj).ctype, 1))[0] == obj % 1, obj
#/mod #
# floordiv #
def test_floordiv():
for obj in gen_obj("#MV__fF"):
fassert(obj.__floordiv__, (1,))
#/floordiv #
# divmod #
def test_divmod():
for obj in gen_obj("#MV__fF"):
fassert(obj.__divmod__, (1,))
#/divmod #
# pow #
def test_pow():
for obj in gen_obj("#p#MV__fF"):
fassert(obj.__pow__, (obj,))
fassert(obj.__pow__, (obj, obj))
assert (glm.array(obj) ** glm.array(obj))[0] == obj ** obj, obj
assert (glm.array(obj) ** obj)[0] == obj ** obj, obj
assert (obj ** glm.array(obj))[0] == obj ** obj, obj
#/pow #
# matmul #
def test_matmul():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSU"):
try:
obj @ obj
except TypeError:
pass
#/matmul #
# iadd #
def test_iadd():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSUB"):
fassert(obj.__iadd__, (obj,))
arr = glm.array(glm.mat4())
fassert(arr.iconcat, (arr,))
fassert(arr.iconcat, (arr,))
#/iadd #
# isub #
def test_isub():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSU"):
fassert(obj.__isub__, (obj,))
#/isub #
# imul #
def test_imul():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSUB"):
fassert(obj.__imul__, (1,))
arr = glm.array(glm.mat4())
fassert(arr.irepeat, (3,))
fassert(arr.irepeat, (3,))
#/imul #
# idiv #
def test_idiv():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSU"):
fassert(obj.__itruediv__, (1,))
#/idiv #
# imod #
def test_imod():
for obj in gen_obj("#MV__fF"):
fassert(obj.__imod__, (1,))
#/imod #
# ifloordiv #
def test_ifloordiv():
for obj in gen_obj("#MV__fF"):
fassert(obj.__ifloordiv__, (1,))
#/ifloordiv #
# ipow #
def test_ipow():
for obj in gen_obj("#MV__fF"):
fassert(obj.__ipow__, (obj,))
#/ipow #
# imatmul #
def test_imatmul():
for obj in gen_obj("#MV_M_Q__fFiqsuIQSU"):
try:
obj @= obj
except TypeError:
pass
#/imatmul #
# str #
def test_str():
for obj in gen_obj("#MV_M_Q"):
assert str(obj), obj
#/str #
# repr #
def test_repr():
for obj in gen_obj("#MV_M_Q"):
assert repr(obj), obj
#/repr #
# len #
def test_len():
for obj in gen_obj("#MV_M_Q"):
assert len(obj), obj
arr = glm.array(glm.mat4())
arr2 = arr.concat(arr)
assert len(arr) == 1, arr
assert len(arr2) == 2, arr2
#/len #
# getitem #
def test_getitem():
for obj in gen_obj("#MV_M_Q"):
for i in range(len(obj)):
assert obj[i] != None, obj
arr = glm.array(glm.mat4(), glm.mat4(2))
assert arr[0] == glm.mat4(), arr
assert arr[1] == glm.mat4(2) == arr[-1], arr
arr = glm.array(*glm.mat4())
assert arr[:] == arr, arr
assert arr[1:] == glm.array(glm.vec4(0,1,0,0),glm.vec4(0,0,1,0),glm.vec4(0,0,0,1)), arr
assert arr[::2] == glm.array(glm.vec4(1,0,0,0),glm.vec4(0,0,1,0)), arr
assert arr[1::2] == glm.array(glm.vec4(0,1,0,0),glm.vec4(0,0,0,1)), arr
#/getitem #
# setitem #
def test_setitem():
for obj in gen_obj("#MV_M_Q"):
for i in range(len(obj)):
fassert(obj.__setitem__,(i, obj[i]))
arr = glm.array(glm.mat4(), glm.mat4(2))
arr[0] = glm.mat4(3)
assert arr[0] == glm.mat4(3), arr
del arr[0]
assert arr == glm.array(glm.mat4(2)), arr
arr = glm.array(*glm.mat4())
del arr[::2]
assert arr == glm.array(glm.vec4(0,1,0,0),glm.vec4(0,0,0,1)), arr
arr[:] = glm.array(glm.vec4(), glm.vec4(2))
assert arr == glm.array(glm.vec4(), glm.vec4(2)), arr
#/setitem #
# contains #
def test_contains():
for obj in gen_obj("#MV_M_Q"):
assert obj[0] in obj, obj
arr = glm.array(glm.mat4(), glm.mat4(2))
assert glm.mat4(2) in arr and not glm.mat4(3) in arr, arr
#/contains #
# Richcompare #
## EQ
def test_EQ():
for obj in gen_obj("V_M_Q"):
assert obj == type(obj)(obj), obj
arr = glm.array(glm.mat4(), glm.mat4(2))
arr2 = glm.array(glm.mat4(), glm.mat4(2))
arr3 = glm.array(glm.mat4(), glm.mat4(2), glm.mat4(0))
assert arr == arr2 and not arr == arr3, (arr, arr2, arr3)
## NE
def test_NE():
for obj in gen_obj("#uV_M__fFiqsuIQSU"):
assert obj != (obj + 1), obj
for obj in gen_obj("V_M__B"):
assert obj != (not obj), obj
for obj in gen_obj("Q"):
assert obj != (1,0,0,0)
arr = glm.array(glm.mat4(), glm.mat4(2))
arr2 = glm.array(glm.mat4(), glm.mat4(2))
arr3 = glm.array(glm.mat4(), glm.mat4(2), glm.mat4(0))
assert not arr != arr2 and arr != arr3, (arr, arr2, arr3)
## LT
def test_LT():
for obj in gen_obj("#uV__fFiqsuIQSU"):
assert all(obj < (obj + 1)) and not any(obj < obj), obj
## LE
def test_LE():
for obj in gen_obj("#uV__fFiqsuIQSU"):
assert all(obj <= obj) and all(obj <= (obj + 1)) and not any((obj + 1) <= obj), obj
## GT
def test_GT():
for obj in gen_obj("#uV__fFiqsuIQSU"):
assert all((obj + 1) > obj) and not any(obj > obj), obj
## GE
def test_GE():
for obj in gen_obj("#uV__fFiqsuIQSU"):
assert all(obj >= obj) and all((obj + 1) >= obj) and not any(obj >= (obj + 1)), obj
#/Richcompare #
# iter #
def test_iter():
for obj in gen_obj("V_M_Q"):
fassert(iter, (obj,))
arr = glm.array(glm.mat4(), glm.mat4(2))
assert list(arr) == arr.to_list(), arr
#/iter #
# hash #
def test_hash():
for obj in gen_obj("V_M_Q"):
fassert(hash, (obj,))
arr = glm.array(glm.mat4(), glm.mat4(2))
assert hash(arr), arr
#/hash #
# buffer protocol #
def check_buffer_protocol(type_, shape, format):
obj = type_()
memview = memoryview(obj)
assert shape == memview.shape
assert format == memview.format
def test_buffer_protocol():
for t, s, f in (
(glm.vec1, (1,), "f"),
(glm.vec2, (2,), "f"),
(glm.vec3, (3,), "f"),
(glm.vec4, (4,), "f"),
(glm.dvec1, (1,), "d"),
(glm.dvec2, (2,), "d"),
(glm.dvec3, (3,), "d"),
(glm.dvec4, (4,), "d"),
(glm.ivec1, (1,), "i"),
(glm.ivec2, (2,), "i"),
(glm.ivec3, (3,), "i"),
(glm.ivec4, (4,), "i"),
(glm.uvec1, (1,), "I"),
(glm.uvec2, (2,), "I"),
(glm.uvec3, (3,), "I"),
(glm.uvec4, (4,), "I"),
(glm.i8vec1, (1,), "b"),
(glm.i8vec2, (2,), "b"),
(glm.i8vec3, (3,), "b"),
(glm.i8vec4, (4,), "b"),
(glm.u8vec1, (1,), "B"),
(glm.u8vec2, (2,), "B"),
(glm.u8vec3, (3,), "B"),
(glm.u8vec4, (4,), "B"),
(glm.i16vec1, (1,), "h"),
(glm.i16vec2, (2,), "h"),
(glm.i16vec3, (3,), "h"),
(glm.i16vec4, (4,), "h"),
(glm.u16vec1, (1,), "H"),
(glm.u16vec2, (2,), "H"),
(glm.u16vec3, (3,), "H"),
(glm.u16vec4, (4,), "H"),
(glm.i64vec1, (1,), "q"),
(glm.i64vec2, (2,), "q"),
(glm.i64vec3, (3,), "q"),
(glm.i64vec4, (4,), "q"),
(glm.u64vec1, (1,), "Q"),
(glm.u64vec2, (2,), "Q"),
(glm.u64vec3, (3,), "Q"),
(glm.u64vec4, (4,), "Q"),
(glm.bvec1, (1,), "?"),
(glm.bvec2, (2,), "?"),
(glm.bvec3, (3,), "?"),
(glm.bvec4, (4,), "?"),
(glm.mat2x2, (2, 2), "f"),
(glm.mat2x3, (3, 2), "f"),
(glm.mat2x4, (4, 2), "f"),
(glm.mat3x2, (2, 3), "f"),
(glm.mat3x3, (3, 3), "f"),
(glm.mat3x4, (4, 3), "f"),
(glm.mat4x2, (2, 4), "f"),
(glm.mat4x3, (3, 4), "f"),
(glm.mat4x4, (4, 4), "f"),
(glm.dmat2x2, (2, 2), "d"),
(glm.dmat2x3, (3, 2), "d"),
(glm.dmat2x4, (4, 2), "d"),
(glm.dmat3x2, (2, 3), "d"),
(glm.dmat3x3, (3, 3), "d"),
(glm.dmat3x4, (4, 3), "d"),
(glm.dmat4x2, (2, 4), "d"),
(glm.dmat4x3, (3, 4), "d"),
(glm.dmat4x4, (4, 4), "d"),
(glm.imat2x2, (2, 2), "i"),
(glm.imat2x3, (3, 2), "i"),
(glm.imat2x4, (4, 2), "i"),
(glm.imat3x2, (2, 3), "i"),
(glm.imat3x3, (3, 3), "i"),
(glm.imat3x4, (4, 3), "i"),
(glm.imat4x2, (2, 4), "i"),
(glm.imat4x3, (3, 4), "i"),
(glm.imat4x4, (4, 4), "i"),
(glm.umat2x2, (2, 2), "I"),
(glm.umat2x3, (3, 2), "I"),
(glm.umat2x4, (4, 2), "I"),
(glm.umat3x2, (2, 3), "I"),
(glm.umat3x3, (3, 3), "I"),
(glm.umat3x4, (4, 3), "I"),
(glm.umat4x2, (2, 4), "I"),
(glm.umat4x3, (3, 4), "I"),
(glm.umat4x4, (4, 4), "I"),