aboutsummaryrefslogtreecommitdiff
path: root/t/14_p2g_templated.t
blob: 88611ae5fba5e2398a4c2a6980ea0c6934a9329c (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
#!/usr/bin/guile -s
!#
; Basic p2g template tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Copyright (C) 2008 Omer Zak.
; This library is free software; you can redistribute it and/or
; modify it under the terms of the GNU Lesser General Public
; License as published by the Free Software Foundation; either
; version 2.1 of the License, or (at your option) any later version.
;
; This library is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
; Lesser General Public License for more details.
;
; You should have received a copy of the GNU Lesser General Public License
; along with this library, in a file named COPYING; if not, write to the
; Free Software Foundation, Inc., 59 Temple Place, Suite 330,
; Boston, MA  02111-1307  USA
;
; For licensing issues, contact <w1@zak.co.il>.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
(use-modules (guiletap))
(use-modules (pyguile))

(plan 57)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; p2g_None2SCM_EOL

(is-ok 1 "default None" '()
	    (python-eval "None" #t))

(is-ok 2 "default None 2" '()
	    (python-eval "None" python2guile))

(is-ok 3 "default None 3" '()
	    (python-eval "None" p2g_None2SCM_EOL))

(is-ok 4 "default None 4" '()
	    (python-eval "None" (cons p2g_None2SCM_EOL '())))

(is-ok 5 "None becomes 'None'" "None"
	    (python-eval "None" (cons p2g_None2SCM_EOL "None")))

(is-ok 6 "non-None" "#<undefined>"
	    (object->string (python-eval "True" p2g_None2SCM_EOL)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; p2g_Bool2SCM_BOOL

(is-ok 7 "p2g_Bool2SCM_BOOL True" #t
       (python-eval "True" p2g_Bool2SCM_BOOL))

(is-ok 8 "p2g_Bool2SCM_BOOL False" #f
       (python-eval "False" p2g_Bool2SCM_BOOL))

(is-ok 9 "p2g_Bool2SCM_BOOL Other" "#<undefined>"
       (object->string (python-eval "2239" p2g_Bool2SCM_BOOL)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; p2g_Int2num, p2g_Long2bignum

(is-ok 10 "p2g_Int2num ok" 12
       (python-eval "12" p2g_Int2num))

(is-ok 11 "p2g_Int2num bad" "#<undefined>"
       (object->string (python-eval "None" p2g_Int2num)))

(is-ok 12 "p2g_Int2num long" "#<undefined>"
       (object->string (python-eval "1000200030004" p2g_Int2num)))

(is-ok 13 "p2g_Long2bignum ok" 12
       (python-eval "12L" p2g_Long2bignum))

(is-ok 14 "p2g_Long2bignum bad" "#<undefined>"
       (object->string (python-eval "'q'" p2g_Long2bignum)))

(is-ok 15 "p2g_Long2bignum long" "1000200030004"
       (object->string (python-eval "1000200030004" p2g_Long2bignum)))

; p2g_leaf

(is-ok 16 "p2g_Int2num+p2g_Long2bignum" "5000600070008"
       (object->string (python-eval "5000600070008" (list p2g_leaf p2g_Int2num p2g_Long2bignum))))

(is-ok 17 "p2g_Int2num+p2g_Long2bignum/cons" "5100600070008"
       (object->string (python-eval "5100600070008" (cons p2g_leaf p2g_Long2bignum))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; float and complex

(is-ok 18 "p2g_Float2real" 2.718
       (python-eval "2.7+0.018" p2g_Float2real))

(is-ok 19 "p2g_Complex2complex" 1.5+3.6i
       (python-eval "1.5+3.6j" p2g_Complex2complex))

; Tuple2list of numbers

(is-ok 20 "Numbers in tuple" '(1023 -445 4.75 -8.1e5 2e7+3.125i)
       (python-eval "(1023,-445,4.75,-8.1e5,(2e7+3.125j))"
		    (list p2g_apply p2g_Tuple2list (list p2g_leaf p2g_Int2num p2g_Long2bignum p2g_Float2real p2g_Complex2complex))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Strings

(is-ok 21 "String" "a message"
       (python-eval "'''a message'''" p2g_String2string))

(is-ok 22 "Sumbol" 'a-message
       (python-eval "'''a-message'''" p2g_String2symbol))

(is-ok 23 "Keyword" #:another-message
       (python-eval "'''another-message'''" p2g_String2keyword))

; string to char

(is-ok 24 "single char" #\space
       (python-eval "' '" p2g_1String2char))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 2-tuple/2-list to pairs

(is-ok 25 "2-tuple to pair" '(12.0 . 34)
       (python-eval "(12.0,34)" (cons p2g_2Tuple2pair (cons p2g_Float2real p2g_Int2num))))

(is-ok 26 "2-tuple to pair, wrong template" "#<undefined>"
       (object->string (python-eval "(12.0,34)" (cons p2g_2Tuple2pair (cons p2g_Int2num p2g_Float2real)))))

(is-ok 27 "2-tuple to pair, wrong template due another reason" "#<undefined>"
       (object->string (python-eval "[12.0,34]" (cons p2g_2Tuple2pair (cons p2g_Int2num p2g_Float2real)))))

(is-ok 28 "2-list to pair" '("ab" . "cd")
       (python-eval "['ab','cd']" (cons p2g_2List2pair (cons p2g_String2string p2g_String2string))))

(is-ok 29 "2-tuple (or 2-list) to pair" '("ef" . 3)
       (python-eval "('ef',3)"
		    (cons p2g_leaf
			  (list
			   (cons p2g_2Tuple2pair (cons p2g_String2string p2g_Int2num))
			   (cons p2g_2List2pair (cons p2g_String2string p2g_Int2num))))))


(is-ok 30 "(2-tuple or) 2-list to pair" '("ef" . 3)
       (python-eval "['ef',3]"
		    (cons p2g_leaf
			  (list
			   (cons p2g_2Tuple2pair (cons p2g_String2string p2g_Int2num))
			   (cons p2g_2List2pair (cons p2g_String2string p2g_Int2num))))))

(is-ok 31 "2-list to pair, wrong template" "#<undefined>"
       (object->string (python-eval "[12.0,34]" (cons p2g_2List2pair (cons p2g_Int2num p2g_Float2real)))))

(is-ok 32 "2-list to pair, wrong template due another reason" "#<undefined>"
       (object->string (python-eval "(12.0,34)" (cons p2g_2List2pair (cons p2g_Int2num p2g_Float2real)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Aggregates - N-Tuples/N-Lists to lists

; from Tuples
(is-ok 33 "tuple to list, mixed datatypes" "(11 12.0 13 34.5 38 780.25)"
       (object->string (python-eval "(11,12.0,13,34.5,38,780.25)" (cons p2g_Tuple2list (list p2g_Int2num p2g_Float2real)))))

(is-ok 34 "tuple to list, wrong data type" "#<undefined>"
       (object->string (python-eval "[11,12.0,13,34.5,38,780.25]" (cons p2g_Tuple2list (list p2g_Int2num p2g_Float2real)))))

(is-ok 35 "tuple to list, P2G_SMOB" "(11 12 13 34 38 780)"
       (object->string (python-eval "(11,12,13,34,38,780)" (cons p2g_Tuple2list p2g_Int2num))))

(is-ok 36 "tuple to list, P2G_SMOB, one bad value" "#<undefined>"
       (object->string (python-eval "(11,12,'a13',34,38,780)" (cons p2g_Tuple2list p2g_Int2num))))

; from Lists
(is-ok 37 "List to list, mixed datatypes" "(11 12.0 13 34.5 38 780.25)"
       (object->string (python-eval "[11,12.0,13,34.5,38,780.25]" (cons p2g_List2list (list p2g_Int2num p2g_Float2real)))))

(is-ok 38 "List to list, wrong data type" "#<undefined>"
       (object->string (python-eval "(11,12.0,13,34.5,38,780.25)" (cons p2g_List2list (list p2g_Int2num p2g_Float2real)))))

(is-ok 39 "List to list, P2G_SMOB" "(11 12 13 34 38 780)"
       (object->string (python-eval "[11,12,13,34,38,780]" (cons p2g_List2list p2g_Int2num))))

(is-ok 40 "List to list, P2G_SMOB, one bad value" "#<undefined>"
       (object->string (python-eval "[11,12,'a13',34,38,780]" (cons p2g_List2list p2g_Int2num))))

; p2g_Dict2alist

(define alist-properly-included?
  (lambda (included includor)
    (if (null? included) #t
	(let ((key (caar included))
	      (value (cdar included))
	      (rest (cdr included)))
	  (let ((includor-ref (assoc key includor)))
	    (cond ((not includor-ref) #f)
		  ((not (equal? (cdr includor-ref) value)) #f)
		  (else (alist-properly-included? rest includor))))))))

(define alist-equal?
  (lambda (alista alistb)
    (and (alist-properly-included? alista alistb)
	 (alist-properly-included? alistb alista))))

; Quick tests of alist-properly-included?
(ok 41 "should pass 1"
    (alist-equal? '((1 . 2)("3" . "4"))
		  '(("3" . "4")(1 . 2))))

(ok 42 "should pass 2"
    (alist-equal? '((1 . 2)("3" . "4"))
		  '((1 . 2)("3" . "4"))))

(ok 43 "should fail 1"
    (not (alist-equal? '((1 . 2)("3" . "4"))
		       '((11 . 2)("3" . "4")))))

(ok 44 "should fail 2"
    (not (alist-equal? '((1 . 2)("3" . "4"))
		  '((1 . 21)("3" . "4")))))

(ok 45 "should fail 3"
    (not (alist-equal? '((1 . 2)("3" . "4"))
		       '((1 . 2)("3a" . "4")))))

(ok 46 "should fail 4"
    (not (alist-equal? '((1 . 2)("3" . "4"))
		       '((1 . 2)("3" . "4b")))))

(ok 47 "should fail 1a"
    (not (alist-equal? '(("3" . "4")(1 . 2))
		       '((11 . 2)("3" . "4")))))

(ok 48 "should fail 2a"
    (not (alist-equal? '(("3" . "4")(1 . 2))
		       '((1 . 21)("3" . "4")))))

(ok 49 "should fail 3a"
    (not (alist-equal? '(("3" . "4")(1 . 2))
		       '((1 . 2)("3a" . "4")))))

(ok 50 "should fail 4a"
    (not (alist-equal? '(("3" . "4")(1 . 2))
		       '((1 . 2)("3" . "4b")))))

(ok 51 "should fail 5"
    (not (alist-equal? '(("3" . "4")(1 . 2)(5 . 6))
		       '((1 . 2)("3" . "4")))))

(ok 52 "should fail 5a"
    (not (alist-equal? '(("3" . "4")(1 . 2)(5 . 6))
		       '((1 . 2)("3" . "4")(7 . 8)(5 . 6)))))


; Proper p2g_Dict2alist tests


(ok 53 "Default p2g_Dict2alist"
    (alist-equal? '((1 . 2) ("3" . "4"))
		  (python-eval "{1 : 2, '3' : '4'}" #t)))

(ok 54 "Explicit p2g_Dict2alist template"
    (alist-equal? '((#\b . 42) (gg . 3))
		  (python-eval "{'b' : None, 'gg' : 3}"
			       (cons p2g_Dict2alist
				     (cons
				      (cons p2g_leaf (list p2g_1String2char p2g_String2symbol))
				      (cons p2g_leaf (list (cons p2g_None2SCM_EOL 42) p2g_Int2num)))))))

(ok 55 "P2G_SMOBP based p2g_Dict2alist template"
    (alist-equal? '(("key1" . val1) ("key2" . myval2) ("k3" . yourval3))
		  (python-eval "{'key1' : 'val1', 'key2' : 'myval2', 'k3' : 'yourval3'}"
			       (cons p2g_Dict2alist
				     (cons p2g_String2string p2g_String2symbol)))))

(is-ok 56 "p2g_Dict2alist key conversion failure" "#<undefined>"
       (object->string (python-eval "{'b' : None, 1.2 : 3}"
				    (cons p2g_Dict2alist
					  (cons
					   (cons p2g_leaf (list p2g_1String2char p2g_String2symbol))
					   (cons p2g_leaf (list (cons p2g_None2SCM_EOL 42) p2g_Int2num)))))))

(is-ok 57 "p2g_Dict2alist value conversion failure" "#<undefined>"
       (object->string (python-eval "{'b' : 'None', 'gg' : 3}"
				    (cons p2g_Dict2alist
					  (cons
					   (cons p2g_leaf (list p2g_1String2char p2g_String2symbol))
					   (cons p2g_leaf (list (cons p2g_None2SCM_EOL 42) p2g_Int2num)))))))

; End of 14_p2g_templated.t