aboutsummaryrefslogtreecommitdiff
path: root/externals/gridflow/base/test.rb
blob: 86c17af3371303cf8580f9a7ee62524849815533 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
# $Id: test.rb,v 1.2 2006-03-15 04:37:28 matju Exp $

$:.delete_if {|x| x=='.' }
require "gridflow"

include GridFlow
GridFlow.verbose=true

$imdir = "./images"
$animdir = "./images/movies"
srand Time.new.to_i
$port = 4200+rand(100)

def pressakey; puts "press return to continue."; readline; end

FO = FObject # shortcut

class Expect < FO
	def praise(*a)
		#raise(*a)
		puts a
	end
	def expect(*v)
		@count=0
		@v=v
		@expecting=true
		yield
		@expecting=false
		praise "wrong number of messages (#{@count}), expecting #{@v.inspect}" if @count!=@v.length
	end
	def _0_list(*l)
		return if not @expecting
		praise "wrong number of messages (#{@count})" if @count==@v.length
		praise "got #{l.inspect} expecting #{@v.inspect}" if @v[@count]!=l
		@count+=1
	end
	def method_missing(s,*a)
		praise "stray message: #{s}: #{a.inspect}"
	end
	install "expect", 1, 0
end

def cast value, type
	case type
	when :b, :u8,:uint8; value & 0xff
	when :s,:i16,:int16; (value & 0x7fff) - (value & 0x8000)
	when :i,:i32,:int32; value
	when :l,:i64,:int64; value
	when :f,:f32,:float32; value.to_f
	when :d,:f64,:float64; value.to_f
	when :r,:ruby; value
	else raise "hell"
	end
end

def test_bitpacking
	#!@#$ WRITE ME
end

def test_numops
	#!@#$ WRITE ME
end

#def tnt() for nt in [:b,:s,:i,:l,:f,:d,:r] do yield end end
def _(o,s,i,d) o.connect(s,i,d) end
def chain(*a)
	(a.length-1).times {|i| a[i].connect 0,a[i+1],a }
	a[-1]
end

def test_math
for nt in [:b,:s,:i,:l,:f,:d,:r] do
	hm = "#".intern
	#GridFlow.verbose = false

	(e=FO["#export_list"])
	(x=Expect.new)
	_ e,0,x,0 

	x.expect([1,2,3,11,12,13,21,22,23]) {
		e.send_in 0, 3,3,nt,hm,1,2,3,11,12,13,21,22,23 }

	(a=FO["fork"])
	(b=FO["@ +"])
	_ a,0,b,0
	_ a,1,b,1
	_ b,0,e,0
	x.expect([4]) { a.send_in 0, 2 }

	x.expect([2,3,5,7]) { e.send_in 0,:list,nt,2,3,5,7 }
	a = FO["#fold + , seed {#{nt} # 0}"]
	_ a,0,e,0
	x.expect([cast(420000,nt)]) { a.send_in 0,"10000 #{nt} # 42" }

	a = FO["# + {#{nt} 0 10}"]
	_ a,0,e,0
	x.expect([1,12,4,18,16,42,64]) {
		a.send_in 0,:list,nt, 1,2,4,8,16,32,64 }

	a = FO["# + {#{nt} 2 3 5}"]
	b = FO["#fold + , seed {#{nt} # 0}"]
	_ a,0,b,0
	_ b,0,e,0
	x.expect([cast(45332,nt)]) { a.send_in 0, 1000,nt,hm,42 }

	a = FO["@ + {#{nt} # 42}"]
	_ a,0,e,0
	x.expect((43..169).to_a) {
		a.send_in 0,:list,nt, *(1..127).to_a }

	x.expect([3,5,9,15]) {
		a.send_in 1,:list,4,nt,hm, 2,3,5,7
		a.send_in 0,:list,4,nt,hm, 1,2,4,8 }
	x.expect([11,12,14,18]) {
		a.send_in 1, "list #{nt} # 10"
		a.send_in 0,:list,nt, 1,2,4,8 }

if nt!=:b and nt!=:f and nt!=:d
	a=FO["# / {#{nt} # 3}"  ]; _ a,0,e,0; x.expect([-2,-1,-1,-1,0,0,0,0,0,1,1,1,2]) { a.send_in(0,:list,nt, *(-6..6).to_a) }
	a=FO["# div {#{nt} # 3}"]; _ a,0,e,0; x.expect([-2,-2,-2,-1,-1,-1,0,0,0,1,1,1,2]) { a.send_in(0, :list, nt, *(-6..6).to_a) }
end

	(a = FO["# ignore {#{nt} # 42}"]).connect 0,e,0
	x.expect((42..52).to_a) { a.send_in(0, :list, nt, *(42..52).to_a) }

	(a = FO["# put {#{nt} # 42}"]).connect 0,e,0
	x.expect([42]*13) { a.send_in(0, :list, nt, *(-6..6).to_a) }

if nt!=:b
	(a = FO["# abs-"]).connect 0,e,0
	x.expect([2,3,5,7]) {
		a.send_in 0,:list,nt, -2,3,-5,7 }
end

	(a = FO["#fold *, seed {#{nt} # 1}"]).connect 0,e,0
	x.expect([210]) { a.send_in 0,:list,nt, 2,3,5,7 }
	x.expect([128]) { a.send_in 0,:list,nt, 1,1,2,1,2,2,2,1,1,2,1,2,2 }

	(a = FO["#fold +, seed {#{nt} 0 0}"]).connect 0,e,0
	x.expect([18,23]) { a.send_in 0, 3,2,nt,hm,2,3,5,7,11,13 }

	(a = FO["#scan +, seed {#{nt} 0 0}"]).connect 0,e,0
	x.expect([2,3,7,10,18,23]) { a.send_in 0, 3,2,nt,hm,2,3,5,7,11,13 }

	(a = FO["#scan *, seed {#{nt} # 1}"]).connect 0,e,0
	x.expect([2,6,30,210]) { a.send_in 0,:list,nt, 2,3,5,7 }
	x.expect([1,1,2,2,4,8,16,16,16,32,32,64,128]) {
		a.send_in 0,:list,nt, 1,1,2,1,2,2,2,1,1,2,1,2,2 }

	(a = FO["#scan +, seed {#{nt} 0 0 0}"]).connect 0,e,0
	x.expect([1,2,3,5,7,9,12,15,18]) {
		a.send_in 0,:list,3,3,nt,hm,*(1..9).to_a }

	(a = FO["#scan +, seed {#{nt} # 0}"]).connect 0,e,0
	x.expect([1,3,6, 4,9,15, 7,15,24]) {
		a.send_in 0,:list,3,3,nt,hm,*(1..9).to_a }

	(a = FO["#outer +"]).connect 0,e,0
	x.expect([9,10,12,17,18,20,33,34,36]) {
		a.send_in 1,:list,nt, 1,2,4
		a.send_in 0,:list,nt, 8,16,32 }

	x.expect((0...100).to_a) {
		a.send_in 1,(0...10).to_a
		a.send_in 0,(0...10).map{|i| 10*i }}

if nt!=:b and nt!=:f and nt!=:d
	(a = FO["#outer",:%,[nt,3,-3]]).connect 0,e,0
	x.expect([0,0,1,-2,2,-1,0,0,1,-2,2,-1,0,0]) {
		a.send_in 0,:list,nt, -30,-20,-10,0,+10,+20,+30 }

	(a = FO["#outer","swap%".intern,[nt,3,-3]]).connect 0,e,0
	x.expect([-27,-3,-17,-3,-7,-3,0,0,3,7,3,17,3,27]) {
		a.send_in 0,:list,nt, -30,-20,-10,0,+10,+20,+30 }
end

	(a = FO["#import {3}"]).connect 0,e,0; x.expect([2,3,5]) { [2,3,5].each {|v| a.send_in 0,:list,nt,hm,v }}
	(a = FO["#import {3}"]).connect 0,e,0; x.expect([2,3,5]) { [2,3,5].each {|v| a.send_in 0,:list,nt,v }}
	(a = FO["#redim {5}"]).connect 0,e,0; x.expect([2,3,5,2,3]) { a.send_in 0,:list,2,3,5 }
	(a = FO["#redim {5}"]).connect 0,e,0; x.expect([0,0,0,0,0]) { a.send_in 0,:list }
	(a = FO["#redim {0}"]).connect 0,e,0; x.expect([]) { a.send_in 0,:list,42,37,69 }

	(a = FO["#inner {2 2 #{nt} # 2 3 5 7}, seed {#{nt} # 0}"]).connect 0,e,0
	(i0 = FO["@redim {2 2}"]).connect 0,a,0
	x.expect([12,17,48,68]) { i0.send_in 0,:list,nt, 1,2,4,8 }

	(a = FO["#outer * {3 2 #{nt} # 1 2 3}"]).connect 0,e,0
	b = FO["#dim"]
	c = FO["#export_list"]
	a.connect 0,b,0
	y = Expect.new
	b.connect 0,c,0
	c.connect 0,y,0

	y.expect([2,3,2]) {
		x.expect([1,2,3,1,2,3,10,20,30,10,20,30]) {
			a.send_in 0,:list,nt, 1, 10 }}

	#pr=GridPrint.new
	(b = FO["#redim {5 5}"]).connect 0,e,0
	(a = FO["#convolve, seed {#{nt} # 0}"]).connect 0,b,0
	(i0 = FO["#redim {5 5 1}"]).connect 0,a,0
	(i1 = FO["#redim {3 1}"]).connect 0,a,1
	i1.send_in 1, 3,3
	x.expect([5,6,5,4,4,4,6,7,6,4,3,3,6,7,5,4,2,3,6,6,5,4,3,4,5]) {
		a.send_in 1,:list,3,3,nt,hm, 1,1,1,1,1,1,1,1,1
		i0.send_in 0,:list,nt, 1,1,1,0,0,0 }

	(a = FO["#convolve, seed {#{nt} # 0}"]).connect 0,e,0
	x.expect([1,3,6,4,0]) {
		a.send_in 1, 1,2,nt,hm, 1,1
		a.send_in 0, 1,5,nt,hm, 0,1,2,4,0 }

	(a = FO["#import {4}"]).connect 0,e,0
	x.expect([2,3,5,7]) {
		[2,3,5,7].each {|v| a.send_in 0,v }}
	x.expect([1,2,3],[4,5,6],[7,8,9]) {
		a.send_in 1, :list, 3
		a.send_in 0, :list, *(1..9).to_a}

	for o in ["#store"]
		(a = FO[o]).connect 0,e,0
		a.send_in 1, 5, 4, nt, hm, 1,2,3,4,5
		x.expect([1,2,3,4,4,5,1,2,2,3,4,5]) {
			a.send_in 0, 3,1, hm, 0,2,4 }
		x.expect([1,2,3,4,5]*24) { a.send_in 0, 2,3,0,hm }
		x.expect([1,2,3,4,5]*4)  { a.send_in 0, 0,hm }
		x.expect([1,2,3,4,5]*4)  { a.send_in 0 }
		x.expect([1,2,3,4]) { a.send_in 0,[0] }
		a.send_in 1,:put_at,[0,0]
		a.send_in 1,2,2,nt,hm,6,7,8,9
		x.expect([6,7,3,4, 8,9,2,3, 4,5,1,2, 3,4,5,1, 2,3,4,5]) { a.send_in 0 }
		x.expect([6,7,3,4]) { a.send_in 0,[0] }
		x.expect([8,9,2,3]) { a.send_in 0,[1] }
		a.send_in 1,:put_at,[1,1]
		a.send_in 1,2,2,nt,hm,11,13,17,19
		x.expect([6,7,3,4, 8,11,13,3, 4,17,19,2, 3,4,5,1, 2,3,4,5]) { a.send_in 0 }
	end

	b = FO["#dim"]
	c = FO["#export_list"]
	a.connect 0,b,0
	y = Expect.new
	b.connect 0,c,0
	c.connect 0,y,0

if nt!=:b and nt!=:f and nt!=:d and nt!=:l
	(a = FO["#for {#{nt} # 0} {#{nt} # 10} {#{nt} # 1}"]).connect 0,e,0
	a.connect 0,b,0
	y.expect([10]) {
		x.expect((0...10).to_a) {
			a.send_in 0 } }

	(a = FO["#for {#{nt} # 0} {#{nt} # -10} {#{nt} # 1}"]).connect 0,e,0
	a.connect 0,b,0
	y.expect([0]) { x.expect([]) { a.send_in 0 } }

	(a = FO["#for {#{nt} # 0} {#{nt} # -10} {#{nt} # -1}"]).connect 0,e,0
	a.connect 0,b,0
	y.expect([10]) { x.expect([0,-1,-2,-3,-4,-5,-6,-7,-8,-9]) { a.send_in 0 } }

	(a = FO["#for {#{nt} 0} {#{nt} 10} {#{nt} 1}"]).connect 0,e,0
	a.connect 0,b,0
	y.expect([10,1]) {
		x.expect((0...10).to_a) {
			a.send_in 0 } }

	(a = FO["#for {#{nt} 2 3} {#{nt} 5 7} {#{nt} 1 1}"]).connect 0,e,0
	a.connect 0,b,0
	y.expect([3,4,2]) {
		x.expect([2,3,2,4,2,5,2,6,3,3,3,4,3,5,3,6,4,3,4,4,4,5,4,6]) {
			a.send_in 0 } }
end

	(a = FO["@complex_sq"]).connect 0,e,0
	x.expect([8,0]) { a.send_in 0, 2, 2 }
	x.expect([0,9]) { a.send_in 0, 0, 3 }

	(a = FO["#rotate 3000 {1 2 5}"]).connect 0,e,0
	a.send_in 0, "5 5 # 1000 0 0 0 0 0" 

#if nt==:f or nt==:d
#	(a = FO["@matrix_solve"]).connect 0,e,0
#	x.expect([1,0,0,0,1,0,0,0,1]) { a.send_in 0, 3, 3, nt, hm, 1,0,0,0,1,0,0,0,1 }
#end
	GridFlow.gfpost "ending test for #{nt}"
end # for nt

	(a = FO["#pack 2"]).connect 0,e,0
	x.expect([42,0]) { a.send_in 0,42 }
	x.expect([42,28]) { a.send_in 1,28 }
	x.expect([1313,28]) { a.send_in 0,1313 }

	(a = FO["#pack 3"]).connect 0,e,0
	x.expect([42,0,0]) { a.send_in 0,42 }
	x.expect([42,28,0]) { a.send_in 1,28 }
	x.expect([42,28,-1]) { a.send_in 2,-1 }

	(a = FO["#pack 4"]).connect 0,e,0
	x.expect([42,0,0,0]) { a.send_in 0,42 }
	x.expect([42,0,0,-42]) { a.send_in 3,-42 }

	(a = FO["#pack 5"]).connect 0,e,0
	x.expect([3.5,0,0,0]) { a.send_in 0,3.5 }
	x.expect([3.5,0,0,-3.5]) { a.send_in 3,-3.5 }

	e = FO["#export_list"]
	e.connect 0,x,0

	a = FO["#import per_message"]
	a.connect 0,e,0
	x.expect([1,2,3]) { a.send_in 0,1,2,3 }
	x.expect([102,111,111]) { a.send_in 0,:symbol,:foo }
	x.expect([ 70, 79, 79]) { a.send_in 0,:symbol,:FOO }

	a = FO["@join 1"]
	a.connect 0,e,0
	a.send_in 1,2,2,nt,hm,11,13,17,19
	x.expect([2,3,11,13,5,7,17,19]) { a.send_in 0,2,2,nt,hm,2,3,5,7 }

if nt!=:d
	a.send_in 1, 5,1,nt,hm,42
	y.expect([5,4]) {
		x.expect([2,3,5,42,7,11,13,42,17,19,23,42,29,31,37,42,41,43,47,42]) {
			a.send_in 0, 5,3,nt,hm,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47 }}
end
	a = FO["@join 0"]
	a.connect 0,e,0
	a.send_in 1,2,2,nt,hm,11,13,17,19
	x.expect([2,3,5,7,11,13,17,19]) { a.send_in 0,2,2,nt,hm,2,3,5,7 }

	a = FO["@join 0 {2 2 2 #{nt} # 1 2 3}"]
	a.connect 0,e,0
	a.connect 0,b,0
	y.expect([2,2,2]) { x.expect([1,2,3,1,2,3,1,2]) { a.send_in 0,0,2,2,nt,hm }}

	a = FO["#ravel"]
	b = FO["#dim"]
	be = FO["#export_list"]
	bx = Expect.new
	a.connect 0,e,0
	a.connect 0,b,0
	b.connect 0,be,0
	be.connect 0,bx,0
	bx.expect([9]) {
		x.expect([1,2,3,2,4,6,3,6,9]) {
			o = FO["#outer *"]
			o.connect 0,a,0
			o.send_in 1,1,2,3
			o.send_in 0,1,2,3
		}
	}

	a = FO["#grade"]
	a.connect 0,e,0
	x.expect([0,2,4,6,8,9,7,5,3,1]) { a.send_in 0, 0,9,1,8,2,7,3,6,4,5 }
	x.expect([0,9,1,8,2,7,3,6,4,5]) { a.send_in 0, 0,2,4,6,8,9,7,5,3,1 }
	x.expect([7,6,5,4,3,2,1,0]) { a.send_in 0, 7,6,5,4,3,2,1,0 }

	a = FO["#grade"]
	b = FO["#fold +"]
	a.connect 0,b,0
	b.connect 0,e,0
	x.expect([100*99/2]) { a.send_in 0, (0...100).map { (rand*0x10000).to_i }}
	x.expect([100*99/2]) { a.send_in 0, (0...100).map { (rand*0x10).to_i }}
	x.expect([100*99/2]) { a.send_in 0, (0...100).map { 0 }}

	a = FO["#perspective"]
	a.connect 0,e,0
	c = []
	8.times {|v|
		3.times {|i|
			c << (v[i] * 1000 - 500) + (if i==2 then 2000 else 0 end)
		}
	}
	x.expect([
		-85,-85,85,-85,-85,85,85,85,
		-51,-51,51,-51,-51,51,51,51]) {
			a.send_in 0, 8,3,hm,*c }
			
# regressiontests for past bugs
	a = FO["#inner"] # that's it.
end

def test_rtmetro
	rt = FO["rtmetro 1000"]
	pr = FO["rubyprint"]
	rt.connect 0,pr,0
	GridFlow.post "trying to start the rtmetro"
	rt.send_in 0,1
	$mainloop.timers.after(10.0) {
		GridFlow.post "trying to stop the rtmetro (after 10 sec delay)"
		rt.send_in 0,0
	}
	$mainloop.loop
end

def test_print
	i = FO["#redim {3}"]
	pr = FO["#print"]
#	pr = GridFlow::RubyPrint.new
	i.connect 0,pr,0
	i.send_in 0, 85, 170, 255
	i.send_in 1, 3, 3
	i.send_in 0, 1, 0, 0, 0
	i.send_in 1, 2, 2, 2
	i.send_in 0, 2, 3, 5, 7, 11, 13, 17, 19
end

class Barf < GridObject
	def _0_rgrid_begin
		raise "barf"
	end
	install_rgrid 0
	install "barf", 1, 0
end

def test_nonsense
#	(a = FO["@! abs"]).connect 0,e,0
#	x.expect_error {
#		a.send_in 1, 42,42 }

	a = FO["#import {3}"]
	b = Barf.new
	a.connect 0,b,0
	begin
	  a.send_in 0, 1, 2, 3
	rescue StandardError
	  nil
	else
	  raise "Expected StandardError"
	end
	p b.inlet_dim(0)
end

def test_store
	a = FO["#in file #{$imdir}/teapot.png"]
	b = FO["#store"]
	c = FO["#out x11"]
	a.connect 0,b,1
	a.send_in 0,"cast uint8"
	a.send_in 0
	b.connect 0,c,0
	d = FO["#for {0 0} {256 256} {1 1}"]
	e = FO["# ^ 85"]
	d.connect 0,e,0
	e.connect 0,b,0
	f = FO["fps detailed"]
	c.connect 0,f,0
	pr = FO["rubyprint"]
	f.connect 0,pr,0
	GridFlow.verbose = false
	256.times {|t|
		e.send_in 1,t
		d.send_in 0
	}
end

# generates recursive checkerboard pattern (munchies) in bluish colours.
class Munchies < FPatcher
	@FOs = ["fork","fork","#for 0 64 1","#for 0 64 1","#for 2 5 1",
	"#outer ^","#outer *"]
	@wires = [-1,0,0,0, 0,0,1,0, 1,1,4,0, 4,0,6,1,
	1,0,3,0, 3,0,5,1, 0,0,2,0, 2,0,5,0, 5,0,6,0, 6,0,-1,0 ]
	def initialize() super end
	install "munchies",1,1
end

def test_munchies
	m=Munchies.new
	gout = FO["#out window"]
	m.connect 0,gout,0
	m.send_in 0
	$mainloop.loop
end

def test_image command
	gin = FO["#in"]
	gout = FO["#out window"]
	gin.connect 0,gout,0
#	31.times {
	3.times {
		gin.send_in 0,"open #{command}"
		gout.send_in 0,"timelog 1"
		gin.send_in 0
	}
	FO["#global"].send_in 0, "profiler_dump"
	$mainloop.loop
end

def test_ppm2
	gin = FO["#in"]
	store = FO["#store"]
	pa = FO["#convolve << + 0"]
	pb = FO["# / 9"]
	ra = FO["#redim {3 3}"]
	gout = FO["#out window"]
	gin.connect 0,store,1
	store.connect 0,pa,0
	pa.connect 0,pb,0
	pb.connect 0,gout,0
	ra.connect 0,pa,1
	ra.send_in 0,"0 0"
	gout.send_in 0,"timelog 1"
	gin.send_in 0,"open file #{$imdir}/teapot.png"
#	gin.send_in 0,"open file #{$imdir}/g001.jpg"
	gin.send_in 0
#	40.times { store.send_in 0 }
	loop { store.send_in 0 }
	v4j = FO["#global"]
	v4j.send_in 0,"profiler_dump"
#	$mainloop.loop
end

def test_foo
	foo = FO["#for {0 0} {64 64} {1 1}"]
	che = FO["#checkers"]
	sca = FO["#scale_by {5 3}"]
	out = FO["#out window"]
	foo.connect 0,che,0
	che.connect 0,sca,0
	sca.connect 0,out,0
	foo.send_in 0
	$mainloop.loop
end

def test_anim(*msgs)
	GridFlow.verbose = false
	gin = FO["#in"]
	gout1 = FO["#out window"]
	#gout1 = FO["@out quicktime file test.mov"]
	#gout1.send_in 0, :codec, :jpeg
	fps = FO["fps detailed"]
	rpr = FO["rubyprint"]
	gout1.connect 0,fps,0
	#fps.connect 0,rpr,0
=begin
	gout1 = FO["#downscale_by {3 2}"]
	gout2 = FO["#rgb_to_greyscale"]
	gout3 = FO["#out aalib X11 -height 60 -width 132"]
	gout1.connect 0,gout2,0
	gout2.connect 0,gout3,0
=end

	rpr = FO["rubyprint"]
#	gin.connect 1,rpr,0

	gin.connect 0,gout1,0
=begin
	layer=FO["@layer"]
	gin.connect 0,layer,0
	layer.connect 0,gout1,0
	check=FO["@checkers"]
	phor=FO["@for {0 0} {256 256} {1 1}"]
	phor.connect 0,check,0
	check.connect 0,layer,1
	phor.send_in 0
=end

#	scale = FO["@scale_by 3"]
#	gin.connect 0,scale,0
#	scale.connect 0,gout1,0

#	pr = FO["rubyprint time"]; gout.connect 0,pr,0
	msgs.each {|m| gin.send_in 0,m }
	gin.send_in 0, "cast uint8"
#	gout.send_in 0,"timelog 1"
	d=Time.new
	frames=2000
	frames.times {|n|
		#GridFlow.post "%d", n
		gin.send_in 0
		#gin.send_in 0, rand(1000)
	}
#	loop { gin.send_in 0 }
#	metro = FO["rtmetro 80"]
#	metro.connect 0,gin,0
#	metro.send_in 0,1
#	$mainloop.loop

	d=Time.new-d
	printf "%d frames in %.6f seconds (avg %.6f ms, %.6f fps)\n",
		frames, d, 1000*d/frames, frames/d
#	global.send_in 0,"dfgdfgdkfjgl"
	gout1.send_in 0, :close
	global = FO["@global"]
	global.send_in 0,"profiler_dump"
end

class TestTCP < FO
	attr_accessor :idle
	def initialize
		@idle = true
	end
	def _0_bang
		# GridFlow.gfpost "tick"
		# avoid recursion
		$mainloop.timers.after(0) {
			($in_client.send_in 0; @idle=false) if @idle
		}
	end
	install "tcptest",1,1
end

def test_tcp
	if fork
		# client (is receiving)
		GridFlow.post_header = "[client] "
		$in_client = in1 = FO["@in"]
		out = FO["@out x11"]
		in1.connect 0,out,0
		out.send_in 0,"timelog 1"
		out.send_in 0,"autodraw 2"
		GridFlow.post "test: waiting 1 second"
		sleep 1
		p "HI"
		#in1.send_in 0,"open grid tcp localhost #{$port}"
		in1.send_in 0,"open grid tcp 127.0.0.1 #{$port}"
		p "HI"

		test_tcp = TestTCP.new

		out.connect 0,test_tcp,0
		test_tcp.connect 0,in1,0

		GridFlow.post "entering mainloop..."
		$mainloop.loop
	else
		# server (is sending)
		GridFlow.post_header = "[server] "
		$in1_server = in1 = FO["@in"]
		$in2_server = in2 = FO["@in"]
		$out = out = FO["@out"]
		toggle = 0
		in1.connect 0,out,0
		in2.connect 0,out,0
		in1.send_in 0,"open #{$imdir}/r001.jpg"
		in2.send_in 0,"open #{$imdir}/b001.jpg"
		out.send_in 0,"open grid tcpserver #{$port}"
		out.send_in 0,"type uint8"
		test_tcp = GridFlow::FO.new
		def test_tcp._0_bang
			# GridFlow.post "tick"
			@toggle ||= 0
			# avoid recursion
			$mainloop.timers.after(0.01) {
				if $out.format.stream
					if @toggle==0; $in1_server else $in2_server end.send_in 0
					@toggle ^= 1
				end
				_0_bang
			}
		end
		out.connect 0,test_tcp,0
		test_tcp.send_in 0
		GridFlow.post "entering mainloop..."
		$mainloop.loop
	end
end

def test_layer
	
	gin = FO["@in png file ShaunaKennedy/atmosphere-bleu.png"]
#	gin1 = FO["@in file #{$imdir}/r001.jpg"]
#	gin = FO["@join 2 {240 320 1 # 128}"]
#	gin1.connect 0,gin,0

#	gfor = FO["@for {0 0} {120 160} {1 1}"]
#	gfor = FO["@for {0 0} {240 320} {1 1}"]
	gfor = FO["@for {0 0} {480 640} {1 1}"]
	gche = FO["@checkers"]

	gove = FO["@layer"]
#	gove = FO["@fold + {0 0 0 0}"]
#	gout = FO["@print"]
#	gove = FO["@inner2 * + 0 {3 4 # 1 0 0 0 0}"]
#	gout = FO["@out sdl"]
	gout = FO["@out x11"]

	gin.connect 0,gove,0
	gfor.connect 0,gche,0
	gche.connect 0,gove,1
	gove.connect 0,gout,0

	gfor.send_in 0

	fps = FO["fps detailed"]
	pr = FO["rubyprint"]
	gout.connect 0,fps,0
	fps.connect 0,pr,0

	loop{gin.send_in 0}
#	gin.send_in 0
#	gin1.send_in 0
	$mainloop.loop
end

Images = [
	"png file opensource.png",
	"#{$imdir}/ruby0216.jpg",
	"#{$imdir}/g001.jpg",
#	"#{$imdir}/teapot.tga",
	"grid gzfile #{$imdir}/foo.grid.gz",
	"grid gzfile #{$imdir}/foo2.grid.gz",
#	"videodev /dev/video0",
]

def test_formats_speed
	gin = FO["@in"]
	gout = FO["@out x11"]
	gin.connect 0,gout,0
	GridFlow.verbose=false
	t1=[]
	Images.each {|command|
		gin.send_in 0,"open #{command}"
		t0 = Time.new
		10.times {gin.send_in 0}
		t1 << (Time.new - t0)
		sleep 1
	}
	p t1
end

def test_formats
	gin = FO["@in"]
	gout = FO["@out window"]
	gs = FO["@ + {int16 # 0}"]
	gin.connect 0,gs,0
	gs.connect 0,gout,0
#	GridFlow.verbose=false
	t1=[]
	Images.each {|command|
		GridFlow.post "SENDING open %s", command
		gin.send_in 0,"open #{command}"
		gin.send_in 0,"cast int16"
		# test for load, rewind, load
		5.times {|x| gs.send_in 1, [:int16, '#'.intern, x*128]; gin.send_in 0}
		# test for filehandle leak
		#1000.times { gin.send_in 0,"open #{command}" }
		sleep 1
	}
	p t1	
end

def test_rewind
	gin = FO["@in videodev /dev/video1 noinit"]
	gin.send_in 0, "transfer read"
	gout = FO["@out ppm file /tmp/foo.ppm"]
#	gout = FO["@out x11"]
	gin.connect 0,gout,0
	loop {
		gin.send_in 0
		gout.send_in 0, "rewind"
	}
end

def test_formats_write
	# read files, store and save them, reload, compare, expect identical
	a = FO["@in"]
	b = FO["@out"]; a.connect 0,b,0
	c = FO["@ -"]; a.connect 0,c,1
	d = FO["@in"]; d.connect 0,c,0
	e = FO["@fold +"]; c.connect 0,e,0
	f = FO["@fold +"]; e.connect 0,f,0
	g = FO["@fold +"]; f.connect 0,g,0
	h = FO["@ / 15000"]; g.connect 0,h,0
	i = FO["@export_list"]; h.connect 0,i,0
	x = Expect.new; i.connect 0,x,0
	[
		["ppm file", "#{$imdir}/g001.jpg"],
		["targa file", "#{$imdir}/teapot.tga"],
		["targa file", "#{$imdir}/tux.tga"],
		["jpeg file", "#{$imdir}/ruby0216.jpg"],
		["grid gzfile", "#{$imdir}/foo.grid.gz", "endian little"],
		["grid gzfile", "#{$imdir}/foo.grid.gz", "endian big"],
	].each {|type,file,*rest|
		a.send_in 0, "open #{type} #{file}"
		b.send_in 0, "open #{type} /tmp/patate"
		rest.each {|r| b.send_in 0,r }
		a.send_in 0
		b.send_in 0, "close"
		raise "written file does not exist" if not File.exist? "/tmp/patate"
		d.send_in 0, "open #{type} /tmp/patate"
		x.expect([0]) { d.send_in 0 }
#		d.send_in 0
	}	
end

def test_mpeg_write
	a = FO["@in ppm file /opt/mex/r.ppm.cat"]
	b = FO["@out x11"]
	a.connect 0,b,0
	loop{a.send_in 0}
end

def test_headerless
	gout = FO["@out"]
	gout.send_in 0, "open grid file #{$imdir}/hello.txt"
	gout.send_in 0, "headerless"
	gout.send_in 0, "type uint8"
	gout.send_in 0, "104 101 108 108 111 32 119 111 114 108 100 33 10"
	gout.send_in 0, "close"
	gin = FO["@in"]
	pr = FO["@print"]
	gin.connect 0,pr,0
	gin.send_in 0, "open grid file #{$imdir}/hello.txt"
	gin.send_in 0, "headerless 13"
	gin.send_in 0, "type uint8"
	gin.send_in 0
end


def test_sound
#	o2 = FO["@ * 359"] # @ 439.775 Hz
#	o1 = FO["@for 0 44100 1"] # 1 sec @ 1.225 Hz ?
	o1 = FO["@for 0 4500 1"]
	o2 = FO["@ * 1600"] # @ 439.775 Hz
	o3 = FO["@ sin* 255"]
	o4 = FO["@ gamma 400"]
	o5 = FO["@ << 7"]
	out = FO["@out"]
	o1.connect 0,o2,0
	o2.connect 0,o3,0
	o3.connect 0,o4,0
	o4.connect 0,o5,0
	o5.connect 0,out,0
#	out.send_in 0,"open raw file /dev/dsp"
	out.send_in 0,"open grid file /dev/dsp"
	out.send_in 0,"type int16"
	x=0
	loop {
		o4.send_in 1, x
		o1.send_in 0
		x+=10
	}
end

include Math
def test_polygon
	o1 = FO["@for 0 5 1"]
	o2 = FO["@ * 14400"]
	o3 = FO["@outer + {0 9000}"]
	o4 = FO["@ +"]
	o5 = FO["@ cos* 112"]
	o6 = FO["@ + {120 160}"]
	poly = FO["@draw_polygon + {3 uint8 # 255}"]
if false
	out1 = FO["@cast int32"]
	out2 = FO["@solarize"]
#	out1 = FO["@downscale_by 2 smoothly"]
	out3 = FO["@out x11"]
	out1.connect 0,out2,0
	out2.connect 0,out3,0
else
	out1 = FO["@out x11"]
	fps = FO["fps detailed cpu"]
	out1.connect 0,fps,0
	pr = FO["rubyprint"]
	fps.connect 0,pr,0
end
	store = FO["@store"]; store.send_in 1, "240 320 3 uint8 # 0"
#	store2 = FO["@store"]
	store.connect 0,poly,0
	poly.connect 0,store,1
#	store2.connect 0,store,1
	o1.connect 0,o2,0
	o2.connect 0,o3,0
	o3.connect 0,o4,0
	o4.connect 0,o5,0
	o5.connect 0,o6,0
	o6.connect 0,poly,2
#	cast = FO["@cast int32"]
#	poly.connect 0,cast,0
#	cast.connect 0,out1,0
	poly.connect 0,out1,0
	x=0
	GridFlow.verbose=false
	task=proc {
		o4.send_in 1, 5000*x
		o5.send_in 1, 200+200*sin(x)
		poly.send_in 1,:list,:uint8, *(0..2).map{|i| 4+4*cos(0.2*x+i*PI*2/3) }
		o1.send_in 0
		store.send_in 0
# 		store2.send_in 0
		x+=1
		if x<1000 then $mainloop.timers.after(0.0) {task[]}
		else GridGlobal.new.send_in 0,"profiler_dump"; exit end
	}
	task[]
	$mainloop.loop
end

class FRoute2 < FO
	def initialize(selector)
		@selector = selector.to_s
	end
	def method_missing(sym,*a)
		sym=sym.to_s
		if sym =~ /^_0_(.*)/
			send_out((if $1==@selector then 0 else 1 end), $1.intern, *a)
		else super end
	end
	install "route2", 1, 2
end

def test_aalib
#	gin = FO["@in ppm file #{$imdir}/r001.jpg"]
	gin = FO["@in ppm file #{$animdir}/b.jpg.cat"]
	grey = FO["@rgb_to_greyscale"]
	cont = FO["@ << 1"]
	clip = FO["@ min 255"]
	gout = FO["@out aalib X11"]
	sto = FO["@store"]
	op = FO["aa_fill_with_text {localhost 4242}"]
	filt = FO["route2 grid"]
	gin.connect 0,grey,0
	grey.connect 0,cont,0
	cont.connect 0,clip,0
	clip.connect 0,gout,0
	gout.connect 0,filt,0
	filt.connect 0,sto,1
	sto.connect 0,op,0
	op.connect 0,gout,0
	gout.send_in 0, :autodraw, 0
	GridFlow.verbose = false
	task=proc{
		gin.send_in 0
		gout.send_in 0, :dump
		sto.send_in 0
		gout.send_in 0, :draw
		$mainloop.timers.after(0.1,&task)
	}
	task[]
	$mainloop.loop
end

def test_store2
	o = [
		FO["@for {0 0} {240 320} {1 1}"],
		FO["@ / 2"],
		FO["@ + 0"],
		FO["@ + 0"],
		FO["@store"],
		FO["@out x11"]]
	(0..4).each {|x| o[x].connect 0,o[x+1],0 }
	q = FO["@in ppm file images/r001.jpg"]
	q.connect 0,o[4],1
	q.send_in 0
	o[0].send_in 0
	$mainloop.loop
end

def test_remap
	rem = FO["@remap_image"]
	rot = FO["@rotate 4000"]
	rem.connect 1,rot,0
	rot.connect 0,rem,1
	gin = FO["@in ppm file #{$imdir}/teapot.png"]
	gout = FO["@out x11"]
	gin.connect 0,rem,0
	rem.connect 0,gout,0
	gin.send_in 0
	$mainloop.loop
end

def test_asm
	GridFlow.verbose=false
	a = FO["@in ppm file images/r001.jpg"]
	aa = FO["@cast uint8"]
	b = FO["@store"]
	d = FO["@store"]
	a.connect 0,aa,0
	aa.connect 0,b,1
	aa.connect 0,d,1
	a.send_in 0
	c = FO["@ + {uint8 # 0}"]
	t0 = Time.new; 1000.times {b.send_in 0}; t1 = Time.new-t0
	t1 *= 1
	b.connect 0,c,0
	stuff=proc{
		3.times{
			t0 = Time.new; 1000.times {b.send_in 0}; t2 = Time.new-t0
			t2 *= 1
			GridFlow.post "   %f   %f   %f", t1, t2, t2-t1
		}
	}
	puts "map:"
	stuff[]
	d.connect 0,c,1 # for zip
	d.send_in 0
	puts "zip:"
	stuff[]	
end

def test_metro
	o1 = RtMetro.new(1000,:geiger)
	o2 = RubyPrint.new(:time)
	o1.connect 0,o2,0
	o1.send_in 0,1
	$mainloop.loop
end

def test_outer
	o = FO["@outer + {0}"]
	o.send_in 0, 25, 240, 320, 3, "#".intern, 42
	g = FO["@global"]
	g.send_in 0, :profiler_dump
end

def test_jmax_to_pd filename
	require "gridflow/extra/jmax_format.rb"
	require "gridflow/extra/puredata_format.rb"
	jfr = JMaxFileReader.new(File.open(filename),FO)
	FO.broken_ok = true
	my_patcher = jfr.parse
#	my_patcher.subobjects.each {|x,| x.trigger if LoadBang===x }
#	$mainloop.loop
#	$p=my_patcher; ARGV.clear; load "/home/matju/bin/iruby"
	filename = File.basename filename
	filename[File.extname(filename)]=".pd"
	filename[0,0]="pd_examples/"
	pfw = PureDataFileWriter.new(filename)
	pfw.write_patcher my_patcher
	pfw.close
end

def test_error
	x = FO["@store"]
	x.send_in 0
end

if ARGV[0] then
	name = ARGV.shift
	send "test_#{name}", *ARGV
#	ARGV.each {|a| send "test_#{a}" }
	exit 0
end

#test_polygon
#test_math
#test_munchies
#test_image "grid file #{$imdir}/foo.grid"
#test_image "grid gzfile #{$imdir}/foo.grid.gz"
#test_print
#test_nonsense
#test_ppm2
#test_anim "open file #{$imdir}/g001.jpg"#,"loop 0"
#test_anim "open ppm file #{$animdir}/b.ppm.cat"
#test_anim "open jpeg file #{$imdir}/rgb.jpeg.cat"
#test_anim "open quicktime file BLAH"
#test_anim "open quicktime file #{$imdir}/rgb_uncompressed.mov"
#test_anim "open quicktime file #{$imdir}/test_mjpega.mov"
#test_anim "open ppm gzfile motion_tracking.ppm.cat.gz"
#test_anim "open videodev /dev/video","channel 1","size 480 640"
#test_anim "open videodev /dev/video1 noinit","transfer read"
#test_anim "open videodev /dev/video","channel 1","size 120 160"
#test_anim "open mpeg file /home/matju/net/Animations/washington_zoom_in.mpeg"
#test_anim "open quicktime file /home/matju/Shauna/part_1.mov"
#test_anim "open quicktime file #{$imdir}/gt.mov"
#test_anim "open quicktime file /home/matju/pics/domopers_hi.mov"
#test_anim "open quicktime file /home/matju/net/c.mov"
#test_formats
#test_tcp
#test_sound
#test_metro
#$mainloop.loop

=begin
a = FO["@print"]
a.send_in 0, "3 3 #{nt} # 1 0 0 0"
a.send_in 0, "3 3 3 #{nt} # 1 2 3 4"
a.send_in 0, "base 16"
a.send_in 0, "3 3 3 #{nt} # 255 0 0 0"
=end