Anonymous avatar Anonymous committed 59c0b1d

working;

Comments (0)

Files changed (3)

 	  Ok( Nil, Str.string_after local_bin 1);
       | 'k' -> (* STRING_EXT *)
 	  let len = (int_of_char (String.get local_bin 1))*256 + (int_of_char (String.get local_bin 2)) in
+	    print_int len;
 	    Ok( String( String.sub local_bin 3 len ), Str.string_after local_bin (len+3) );
       | 'l' -> (* LIST_EXT *)
 	  let len = make_int32 (String.sub local_bin 1 5) in
 	  raise Not_supported;
       | 'q' -> (* SMALL_ATOM_EXT *)
 	  let len = int_of_char (String.get local_bin 1) in   
-	    Ok( Atom( String.sub local_bin 3 len ), Str.string_after local_bin (len+3) );
+	    Ok( Atom( String.sub local_bin 2 len ), Str.string_after local_bin (len+2) );
       | 'r' -> (* FUN_EXT *)
 	  raise Not_supported;	    
       | 's' -> (* NEW_FUN_EXT *)
   in
     print_ 0;;
 
-let init_buf buf =
-  String.set buf 0 (char_of_int 131);
-  1;;
-
 (** these funcs have typical signature:
     [encode_* term buffer offset] -> offset + bytes_written **)
+
 let encode_int i buf ofs =
   if i < 256 then (* SMALL_INTEGER_EXT *)
     begin 
 
 let encode_string str buf ofs =   (* STRING_EXT *) 
   let len = String.length str in
-  let tmp_str = Printf.sprintf "k  %s" str in
+  let tmp_str = Printf.sprintf "k12%s" str in
     String.set tmp_str 1 (char_of_int (len/256));
     String.set tmp_str 2 (char_of_int (len mod 256));
     String.blit tmp_str 0 buf ofs (len+3);
-    ofs + (String.length str);;
+    ofs + (String.length tmp_str);;
 
 let encode_binary bin buf ofs = (* BINARY_EXT *)
   let str = Printf.sprintf "mabcd%s" bin in
 let encode_list list = 
   (* LIST_EXT *) "l";;
 
-
+let encode_term term buf ofs =
+  String.set buf ofs (char_of_int 131);
+  match term with
+    | Int(i) ->   encode_int i buf (ofs+1); 
+    | Float(f) -> encode_float f buf (ofs+1);
+    | Atom(a)->   encode_atom a buf (ofs+1);
+    | String(s)-> encode_string s buf (ofs+1);
+    | Binary(b)-> encode_binary b buf (ofs+1);
+    | _ -> raise Unknown_type;; 
 	  [print_termfile "termwriter.bert"];
       | list -> 
 	  List.map print_termfile list;;
-
-let _ = 
-  let str = String.create 128 in
-  let _ = Bert.init_buf str in
-  let i = Bert.encode_int 774567 str 1 in
-  let j = Bert.encode_float 234.354 str i in
-  let k = Bert.encode_atom "HAHAHAHA" str j in
-  let l = Bert.encode_string "HAHAHAHA" str k in
-  let m = Bert.encode_binary "HAHAHAHA" str l in
-    Bert.print_binary_string str;
-(*    print_endline str; *)
-    m;;
 
 exception Test_failure;;
 
-let _ =
-  print_endline "hoge";
-  raise Test_failure;;
+let do_tests suite = 
+  let rec run_tests my_suite suc = 
+    match my_suite with
+      | [] -> (suc = List.length suite);
+      | hd::tl ->
+	  if hd () then run_tests tl (suc+1)
+	  else run_tests tl suc 
+  in
+    run_tests suite 0;;
 
+let rec decoding_test answers problem results=
+  match answers, (Bert.decode_binary problem) with
+    | [], _ -> results;
+    | hd::tl, Bert.Ok( term, remain )->
+	print_endline "!!!";
+	Bert.print_binary_string problem;
+	Bert.print_erlterm term;
+	begin if term = hd then
+	  decoding_test tl remain (results+1)
+	else 
+	  raise Test_failure
+	end;
+    | _ ->
+	raise Test_failure;;
+
+let rec encode_all list str ofs =
+  match list with
+    | [] ->  ofs;
+    | hd::tl ->
+	let nofs = Bert.encode_term hd str ofs in
+	  encode_all tl str nofs;;
+
+let _ = 
+  let list = [ Bert.Int(774567); Bert.Float(23.43); Bert.Atom("HAHAHAHA");
+	       Bert.String("HAHAHA"); Bert.Binary("HAHAHA") ] in
+  let str = String.create 128 in
+  let _ = encode_all list str 0 in
+    Bert.print_binary_string str;
+    decoding_test list str 0 ;;
+(*    print_endline str; 
+  raise Test_failure;; *)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.