Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add MessagePack.pack(); add test_pack.html , rename tests.html => tes…

…t_unpack.html, create directory lib/ test/
  • Loading branch information...
commit 3baaea2dda8f5525130e461e323d7fd38a2cc407 1 parent c636bc6
@cuzic authored
View
0  msgpack.js → lib/msgpack.js
File renamed without changes
View
76 test/server.rb
@@ -0,0 +1,76 @@
+#!/usr/local/bin/ruby
+require 'webrick'
+require 'rubygems'
+require 'msgpack'
+
+def webrick(config = {})
+ WEBrick::HTTPServer.new(config).instance_eval do |server|
+ [:INT, :TERM].each do |signal|
+ Signal.trap(signal) { shutdown }
+ end
+ server.mount_proc("/unpack") do |req, res|
+ if req.path =~ %r(/unpack/([a-z0-9]+)) then
+ path = $1
+ res.content_type = "text/plain; charset=x-user-defined"
+ case path
+ when "null"
+ res.body = nil.to_msgpack
+ when "true"
+ res.body = true.to_msgpack
+ when "false"
+ res.body = false.to_msgpack
+ when "fixnum"
+ res.body = 100.to_msgpack
+ when "fixraw"
+ res.body = "0123456789abc".to_msgpack
+ when "fixarray"
+ res.body = [0, true, false].to_msgpack
+ when "fixmap"
+ res.body = {0 => "0", 1 => "1"}.to_msgpack
+ when "float"
+ res.body = (10.5).to_msgpack
+ when "uint8"
+ res.body = 200.to_msgpack
+ when "uint16"
+ res.body = 40000.to_msgpack
+ when "uint32"
+ res.body = (3*(2**30)).to_msgpack
+ when "uint64"
+ res.body = (3*(2**62)).to_msgpack
+ when "int8"
+ res.body = (-100).to_msgpack
+ when "int16"
+ res.body = (-1000).to_msgpack
+ when "int32"
+ res.body = (-1000_000).to_msgpack
+ when "int64"
+ res.body = (-1000_000_000_000_000).to_msgpack
+ when "raw16"
+ res.body = ("a"*1000).to_msgpack
+ when "raw32"
+ res.body = ("a"*100_000).to_msgpack
+ when "array16"
+ res.body = ([1]*1000).to_msgpack
+ when "array32"
+ res.body = ([1]*100_000).to_msgpack
+ when "map16"
+ hash = (0..1000).inject({}) do |h, key|
+ h[key] = key.to_s
+ h
+ end
+ res.body = hash.to_msgpack
+ when "map32"
+ hash = (0..1000_000).inject({}) do |h, key|
+ h[key] = key.to_s
+ h
+ end
+ res.body = hash.to_msgpack
+ end
+ end
+ end
+ start
+ end
+end
+
+webrick :DocumentRoot => Dir.pwd,
+ :Port => 5001
View
99 test/test_pack.html
@@ -0,0 +1,99 @@
+<html>
+ <head>
+ <title> test </title>
+ <script language="JavaScript" type="text/javascript"
+ src="./jsunit/app/jsUnitCore.js">
+ </script>
+ <script language="JavaScript" type="text/javascript"
+ src="./msgpack.js">
+ </script>
+ <script language="JavaScript" type="text/javascript">
+ if(MessagePack.hasVBS){
+ // quick hack for Internet Explorer unescape();
+ execScript(
+ 'Function msgpack_getByte(data, pos)\r\n' +
+ ' msgpack_getByte = AscB(MidB(data, 2*pos + 1, 1))\r\n'+
+ 'End Function\r\n' , "VBScript"
+ );
+ }
+
+ function _test_pack(value, hex){
+ var data = MessagePack.pack(value);
+ var pp = "\"" + hex.replace(/../g, "\\x$&") + "\""
+ var str = eval(pp);
+ if(value == 0.1){
+ var s = "";
+ for(var i = 0; i < data.length; ++i){
+ s += data.charCodeAt(i).toString(16);
+ }
+ }
+ assertEquals(str, data);
+ }
+
+ function test_pack_positive_fixnum() {
+ _test_pack(0, "00");
+ }
+ function test_pack_negative_fixnum() {
+ _test_pack(-1, "ff");
+ }
+ function test_pack_uint8() {
+ _test_pack(255, "ccff");
+ }
+ function test_pack_FixRaw() {
+ _test_pack("a", "a161");
+ }
+ function test_pack_FixArray() {
+ _test_pack([0], "9100");
+ }
+ function test_pack_FixMap() {
+ _test_pack({"a": 64}, "81a16140");
+ }
+ function test_pack_Nil() {
+ _test_pack(null, "c0");
+ }
+ function test_pack_True() {
+ _test_pack(true, "c3");
+ }
+ function test_pack_False() {
+ _test_pack(false, "c2");
+ }
+ function test_pack_double() {
+ _test_pack(0.1, "cb3fb999999999999a");
+ }
+ function test_pack_uint16() {
+ _test_pack(32768, "cd8000");
+ }
+ function test_pack_uint32() {
+ _test_pack(1048576, "ce00100000");
+ }
+ function test_pack_int8() {
+ _test_pack(-64, "d0c0");
+ }
+ function test_pack_int16() {
+ _test_pack(-1024, "d1fc00");
+ }
+ function test_pack_int32() {
+ _test_pack(-1048576, "d2fff00000");
+ }
+ function test_pack_int64() {
+ _test_pack(-1099511627776, "d3ffffff0000000000");
+ }
+ function test_pack_raw16() {
+ var spaces = " ";
+ _test_pack(spaces, "da002820202020202020202020202020202020202020202020202020202020202020202020202020202020");
+ }
+ function test_pack_array16() {
+ var ary = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
+ _test_pack(ary, "dc001000000000000000000000000000000000");
+ }
+
+ function test_pack_hiragana() {
+ //var data = MessagePack.pack("あい");
+ //assertEquals("hiragana ai", data, "\x");
+ }
+ </script>
+</head>
+<body>
+
+</body>
+</html>
View
0  tests.html → test/test_unpack.html
File renamed without changes
View
195 test/test_xhr.html
@@ -0,0 +1,195 @@
+<html>
+ <head>
+ <title> test xml http request</title>
+ <script language="JavaScript" type="text/javascript"
+ src="./jsunit/app/jsUnitCore.js">
+ </script>
+ <script language="JavaScript" type="text/javascript"
+ src="./msgpack.js">
+ </script>
+ <script language="JavaScript" type="text/javascript">
+ function async_request(url, fn){
+ var xhr;
+ if(window.ActiveXObject) {
+ try {
+ xhr = new ActiveXObject('Msxml2.XMLHTTP');
+ }
+ catch(e) {
+ xhr = new ActiveXObject('Microsoft.XMLHTTP');
+ }
+ }
+ else if(window.XMLHttpRequest) {
+ xhr = new XMLHttpRequest();
+ } else {
+ throw "no xml http request";
+ }
+
+ var callback = function(){
+ if(xhr.readyState == 4) {
+ if(window.ActiveXObject){
+ fn(xhr.responseBody);
+ }
+ else{
+ fn(xhr.responseText);
+ }
+ }
+ };
+
+ xhr.onreadystatechange = callback;
+ if(xhr.overrideMimeType){
+ xhr.overrideMimeType('text/plain; charset=x-user-defined');
+ }
+
+ xhr.open("GET", url, false);
+ xhr.send(null);
+ }
+
+ function _test_unpack(path, value_or_func){
+ var url = "http://localhost:5001/unpack/";
+ var callback = function(msg){
+ var data = MessagePack.unpack(msg);
+ if(typeof(value_or_func) == "function"){
+ var func = value_or_func;
+ func(data);
+ }
+ else{
+ var value = value_or_func;
+ assertEquals(path + ": \"" + escape(data) +
+ "\" == " + value, value, data);
+ }
+ if(value != data){
+ C(value + "!=" + data);
+ }
+ else{
+ C(value + "==" + data);
+ }
+ };
+ async_request(url + path, callback);
+ }
+
+ function testAssert_fixnum() {
+ _test_unpack("fixnum", 100);
+ }
+ function testAssert_null() {
+ _test_unpack("null", function(data){
+ assertNull(data);
+ });
+ }
+ function testAssert_true() {
+ _test_unpack("true", function(data){
+ assertTrue(data);
+ });
+ }
+ function testAssert_fixraw() {
+ _test_unpack("fixraw", "0123456789abc");
+ }
+
+ function testAssert_fixarray(){
+ _test_unpack("fixarray", function(data){
+ assertArrayEquals(data, [0, true, false]);
+ });
+ }
+ function testAssert_fixmap() {
+ _test_unpack("fixmap", function(data){
+ assertObjectEquals(data, {0: "0", 1: "1"});
+ });
+ }
+ function testAssert_float() {
+ _test_unpack("float", 10.5);
+ }
+ function testAssert_uint8() {
+ _test_unpack("uint8", 200);
+ }
+ function testAssert_uint16() {
+ _test_unpack("uint16", 40000);
+ }
+ function testAssert_uint32() {
+ _test_unpack("uint32", 3*Math.pow(2, 30));
+ }
+ function testAssert_uint64() {
+ _test_unpack("uint64", 3*Math.pow(2, 62));
+ }
+ function testAssert_int8() {
+ _test_unpack("int8", -100);
+ }
+ function testAssert_int16() {
+ _test_unpack("int16", -1000);
+ }
+ function testAssert_int32() {
+ _test_unpack("int32", -1000000);
+ }
+ function testAssert_int64() {
+ _test_unpack("int64", -Math.pow(10, 15));
+ }
+ function testAssert_raw16() {
+ var a = "";
+ for(var i = 0; i < 1000; i++){
+ a.concat("a");
+ }
+ _test_unpack("raw16", a);
+ }
+ function testAssert_raw32() {
+ var a = "";
+ for(var i = 0; i < 100000; i++){
+ a.concat("a");
+ }
+ _test_unpack("raw32", a);
+ }
+ function testAssert_array16() {
+ var a = [];
+ for(var i = 0; i < 1000; i++){
+ a[i] = 1;
+ }
+ _test_unpack("array16", function(data){
+ assertArrayEquals(data, a);
+ });
+ }
+
+ function testAssert_array32() {
+ var a = [];
+ for(var i = 0; i < 100000; i++){
+ a[i] = 1;
+ }
+ _test_unpack("array32", function(data){
+ assertArrayEquals(data, a);
+ });
+ }
+ function testAssert_map16() {
+ var a = {};
+ for(var i = 0; i < 1000; i++){
+ a[i] = "" + i;
+ }
+ _test_unpack("map16", function(data){
+ assertObjectEquals(data, a);
+ });
+ }
+
+ function testAssert_map32() {
+ var a = {};
+ for(var i = 0; i < 100000; i++){
+ a[i] = "" + i;
+ }
+ _test_unpack("map32", function(data){
+ assertObjectEquals(data, a);
+ });
+ }
+ </script>
+</head>
+<body>
+ <input type=button onclick="testAssert_fixarray();" value="fixarray">
+ <input type=button onclick="testAssert_uint8();" value="uint8">
+ <input type=button onclick="testAssert_fixnum();" value="fixnum">
+ <input type=button onclick="testAssert_uint16();" value="uint16">
+ <input type=button onclick="testAssert_uint32();" value="uint32">
+ <input type=button onclick="testAssert_float();" value="float">
+ <br/>
+ <textarea id="console" cols=50 rows=10>
+ </textarea>
+ <script type="text/javascript">
+ function C(msg){
+ var ta = document.getElementById("console");
+ ta.value = msg + "\n" + ta.value;
+ }
+ </script>
+</body>
+</html>

0 comments on commit 3baaea2

Please sign in to comment.
Something went wrong with that request. Please try again.