| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Copyright (C) 2006 Chris Double. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | ! 
 | 
					
						
							| 
									
										
										
										
											2009-05-16 01:29:21 -04:00
										 |  |  | USING: tools.test kernel serialize io io.streams.byte-array | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | alien arrays byte-arrays bit-arrays specialized-arrays | 
					
						
							| 
									
										
										
										
											2008-11-14 21:18:16 -05:00
										 |  |  | sequences math prettyprint parser classes math.constants | 
					
						
							| 
									
										
										
										
											2010-02-24 10:50:31 -05:00
										 |  |  | io.encodings.binary random assocs serialize.private alien.c-types | 
					
						
							|  |  |  | combinators.short-circuit ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | SPECIALIZED-ARRAY: double | 
					
						
							| 
									
										
										
										
											2008-03-01 17:00:45 -05:00
										 |  |  | IN: serialize.tests | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-23 01:34:02 -04:00
										 |  |  | : test-serialize-cell ( a -- ? )
 | 
					
						
							| 
									
										
										
										
											2010-01-14 13:48:57 -05:00
										 |  |  |     2^ random dup
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     binary [ serialize-cell ] with-byte-writer | 
					
						
							|  |  |  |     binary [ deserialize-cell ] with-byte-reader = ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ t ] [ | 
					
						
							|  |  |  |     100 [ | 
					
						
							|  |  |  |         drop
 | 
					
						
							| 
									
										
										
										
											2010-02-24 10:50:31 -05:00
										 |  |  |         { | 
					
						
							|  |  |  |             [ 40 [        test-serialize-cell ] all-integers? ] | 
					
						
							|  |  |  |             [  4 [ 40 *   test-serialize-cell ] all-integers? ] | 
					
						
							|  |  |  |             [  4 [ 400 *  test-serialize-cell ] all-integers? ] | 
					
						
							|  |  |  |             [  4 [ 4000 * test-serialize-cell ] all-integers? ] | 
					
						
							|  |  |  |         } 0&& | 
					
						
							| 
									
										
										
										
											2010-01-14 10:10:13 -05:00
										 |  |  |     ] all-integers?
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | TUPLE: serialize-test a b ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <serialize-test> serialize-test | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-23 01:34:02 -04:00
										 |  |  | CONSTANT: objects | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         f
 | 
					
						
							|  |  |  |         t
 | 
					
						
							|  |  |  |         0
 | 
					
						
							|  |  |  |         -50
 | 
					
						
							|  |  |  |         20
 | 
					
						
							|  |  |  |         5.25
 | 
					
						
							|  |  |  |         -5.25
 | 
					
						
							|  |  |  |         C{ 1 2 } | 
					
						
							|  |  |  |         1/2 | 
					
						
							|  |  |  |         "test" | 
					
						
							|  |  |  |         { 1 2 "three" } | 
					
						
							|  |  |  |         V{ 1 2 "three" } | 
					
						
							|  |  |  |         SBUF" hello world" | 
					
						
							| 
									
										
										
										
											2010-08-25 20:51:44 -04:00
										 |  |  |         "hello \u012345 unicode" | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         \ dup | 
					
						
							|  |  |  |         [ \ dup dup ] | 
					
						
							|  |  |  |         T{ serialize-test f "a" 2 } | 
					
						
							|  |  |  |         B{ 50 13 55 64 1 } | 
					
						
							|  |  |  |         ?{ t f t f f t f } | 
					
						
							| 
									
										
										
										
											2008-11-14 21:18:16 -05:00
										 |  |  |         double-array{ 1.0 3.0 4.0 1.0 2.35 0.33 } | 
					
						
							| 
									
										
										
										
											2009-10-28 14:38:27 -04:00
										 |  |  |         << 1 [ 2 ] curry suffix! >> | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         { { "a" "bc" } { "de" "fg" } } | 
					
						
							|  |  |  |         H{ { "a" "bc" } { "de" "fg" } } | 
					
						
							| 
									
										
										
										
											2009-03-23 01:34:02 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : check-serialize-1 ( obj -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     "=====" print
 | 
					
						
							| 
									
										
										
										
											2011-10-24 22:33:09 -04:00
										 |  |  |     dup class-of .
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     dup .
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     dup
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     object>bytes | 
					
						
							|  |  |  |     bytes>object | 
					
						
							|  |  |  |     dup . = ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : check-serialize-2 ( obj -- ? )
 | 
					
						
							|  |  |  |     dup number? over wrapper? or [ | 
					
						
							|  |  |  |         drop t ! we don't care if numbers aren't interned | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |         "=====" print
 | 
					
						
							| 
									
										
										
										
											2011-10-24 22:33:09 -04:00
										 |  |  |         dup class-of .
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |         dup 2array dup .
 | 
					
						
							|  |  |  |         object>bytes | 
					
						
							|  |  |  |         bytes>object dup .
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         first2 eq?
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ t ] [ objects [ check-serialize-1 ] all? ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ t ] [ objects [ check-serialize-2 ] all? ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ t ] [ pi check-serialize-1 ] unit-test | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  | [ serialize ] must-infer | 
					
						
							|  |  |  | [ deserialize ] must-infer | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | [ t ] [ | 
					
						
							|  |  |  |     V{ } dup dup push
 | 
					
						
							|  |  |  |     object>bytes | 
					
						
							|  |  |  |     bytes>object | 
					
						
							|  |  |  |     dup first eq?
 | 
					
						
							|  |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ t ] [ | 
					
						
							|  |  |  |     H{ } dup dup dup set-at
 | 
					
						
							|  |  |  |     object>bytes | 
					
						
							|  |  |  |     bytes>object | 
					
						
							|  |  |  |     dup keys first eq?
 | 
					
						
							|  |  |  | ] unit-test |