mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-13 22:50:19 +02:00
add test/cases.mpac and test/cases_compact.mpac
This commit is contained in:
BIN
test/cases.mpac
Normal file
BIN
test/cases.mpac
Normal file
Binary file not shown.
BIN
test/cases_compact.mpac
Normal file
BIN
test/cases_compact.mpac
Normal file
Binary file not shown.
76
test/cases_gen.rb
Normal file
76
test/cases_gen.rb
Normal file
@@ -0,0 +1,76 @@
|
||||
#
|
||||
# MessagePack format test case
|
||||
#
|
||||
require 'rubygems' rescue nil
|
||||
require 'msgpack'
|
||||
|
||||
source = <<EOF
|
||||
c2 # false
|
||||
c3 # true
|
||||
c0 # nil
|
||||
00 # 0 Positive FixNum
|
||||
cc 00 # 0 uint8
|
||||
cd 00 00 # 0 uint16
|
||||
ce 00 00 00 00 # 0 uint32
|
||||
cf 00 00 00 00 00 00 00 00 # 0 uint64
|
||||
d0 00 # 0 int8
|
||||
d1 00 00 # 0 int16
|
||||
d2 00 00 00 00 # 0 int32
|
||||
d3 00 00 00 00 00 00 00 00 # 0 int64
|
||||
ff # -1 Negative FixNum
|
||||
d0 ff # -1 int8
|
||||
d1 ff ff # -1 int16
|
||||
d2 ff ff ff ff # -1 int32
|
||||
d3 ff ff ff ff ff ff ff ff # -1 int64
|
||||
7f # 127 Positive FixNum
|
||||
cc 7f # 127 uint8
|
||||
cd 00 ff # 255 uint16
|
||||
ce 00 00 ff ff # 65535 uint32
|
||||
cf 00 00 00 00 ff ff ff ff # 4294967295 uint64
|
||||
e0 # -32 Negative FixNum
|
||||
d0 e0 # -32 int8
|
||||
d1 ff 80 # -128 int16
|
||||
d2 ff ff 80 00 # -32768 int32
|
||||
d3 ff ff ff ff 80 00 00 00 # -2147483648 int64
|
||||
#ca 00 00 00 00 # 0.0 float
|
||||
cb 00 00 00 00 00 00 00 00 # 0.0 double
|
||||
#ca 80 00 00 00 # -0.0 float
|
||||
cb 80 00 00 00 00 00 00 00 # -0.0 double
|
||||
cb 3f f0 00 00 00 00 00 00 # 1.0 double
|
||||
cb bf f0 00 00 00 00 00 00 # -1.0 double
|
||||
a1 61 # "a" FixRaw
|
||||
da 00 01 61 # "a" raw 16
|
||||
db 00 00 00 01 61 # "a" raw 32
|
||||
a0 # "" FixRaw
|
||||
da 00 00 # "" raw 16
|
||||
db 00 00 00 00 # "" raw 32
|
||||
91 00 # [0] FixArray
|
||||
dc 00 01 00 # [0] array 16
|
||||
dd 00 00 00 01 00 # [0] array 32
|
||||
90 # [] FixArray
|
||||
dc 00 00 # [] array 16
|
||||
dd 00 00 00 00 # [] array 32
|
||||
80 # {} FixMap
|
||||
de 00 00 # {} map 16
|
||||
df 00 00 00 00 # {} map 32
|
||||
81 a1 61 61 # {"a"=>97} FixMap
|
||||
de 00 01 a1 61 61 # {"a"=>97} map 16
|
||||
df 00 00 00 01 a1 61 61 # {"a"=>97} map 32
|
||||
91 90 # [[]]
|
||||
91 91 a1 61 # [["a"]]
|
||||
EOF
|
||||
|
||||
source.gsub!(/\#.+$/,'')
|
||||
bytes = source.strip.split(/\s+/).map {|x| x.to_i(16) }.pack('C*')
|
||||
|
||||
compact_bytes = ""
|
||||
pac = MessagePack::Unpacker.new
|
||||
pac.feed(bytes)
|
||||
pac.each {|obj|
|
||||
p obj
|
||||
compact_bytes << obj.to_msgpack
|
||||
}
|
||||
|
||||
File.open("cases.mpac","w") {|f| f.write(bytes) }
|
||||
File.open("cases_compact.mpac","w") {|f| f.write(compact_bytes) }
|
||||
|
133
test/crosslang.cc
Normal file
133
test/crosslang.cc
Normal file
@@ -0,0 +1,133 @@
|
||||
//
|
||||
// MessagePack cross-language test tool
|
||||
//
|
||||
// $ cd ../cpp && ./configure && make && make install
|
||||
// or
|
||||
// $ port install msgpack # MacPorts
|
||||
//
|
||||
// $ g++ -Wall -lmsgpack crosslang.cc
|
||||
//
|
||||
#include <msgpack.hpp>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static int run(int infd, int outfd)
|
||||
try {
|
||||
msgpack::unpacker pac;
|
||||
|
||||
while(true) {
|
||||
pac.reserve_buffer(32*1024);
|
||||
|
||||
ssize_t count =
|
||||
read(infd, pac.buffer(), pac.buffer_capacity());
|
||||
|
||||
if(count <= 0) {
|
||||
if(count == 0) {
|
||||
return 0;
|
||||
}
|
||||
if(errno == EAGAIN || errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
pac.buffer_consumed(count);
|
||||
|
||||
msgpack::unpacked result;
|
||||
while(pac.next(&result)) {
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, result.get());
|
||||
|
||||
const char* p = sbuf.data();
|
||||
const char* const pend = p + sbuf.size();
|
||||
while(p < pend) {
|
||||
ssize_t bytes = write(outfd, p, pend-p);
|
||||
|
||||
if(bytes <= 0) {
|
||||
if(count == 0) {
|
||||
return 0;
|
||||
}
|
||||
if(errno == EAGAIN || errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
p += bytes;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void usage(const char* prog)
|
||||
{
|
||||
printf(
|
||||
"Usage: %s [in-file] [out-file]\n"
|
||||
"\n"
|
||||
"This tool is for testing of MessagePack implementation.\n"
|
||||
"This does following behavior:\n"
|
||||
"\n"
|
||||
" 1. Reads objects serialized by MessagePack from <in-file> (default: stdin)\n"
|
||||
" 2. Re-serializes the objects using C++ implementation of MessagePack (Note that C++ implementation is considered valid)\n"
|
||||
" 3. Writes the re-serialized objects into <out-file> (default: stdout)\n"
|
||||
"\n"
|
||||
, prog);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int infd = 0;
|
||||
int outfd = 1;
|
||||
|
||||
if(argc < 1 || argc > 3) {
|
||||
usage(argv[0]);
|
||||
}
|
||||
|
||||
for(int i=1; i < argc; ++i) {
|
||||
if(strlen(argv[i]) > 1 && argv[i][0] == '-') {
|
||||
usage(argv[0]);
|
||||
}
|
||||
}
|
||||
|
||||
if(argc >= 2) {
|
||||
const char* fname = argv[1];
|
||||
if(strcmp(fname, "-") != 0) {
|
||||
infd = open(fname, O_RDONLY);
|
||||
if(infd < 0) {
|
||||
perror("can't open input file");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(argc >= 3) {
|
||||
const char* fname = argv[2];
|
||||
if(strcmp(fname, "-") != 0) {
|
||||
outfd = open(fname, O_WRONLY | O_CREAT| O_TRUNC, 0666);
|
||||
if(outfd < 0) {
|
||||
perror("can't open output file");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int code = run(infd, outfd);
|
||||
|
||||
close(infd);
|
||||
close(outfd);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
88
test/crosslang.rb
Normal file
88
test/crosslang.rb
Normal file
@@ -0,0 +1,88 @@
|
||||
#
|
||||
# MessagePack cross-language test tool
|
||||
#
|
||||
# $ gem install msgpack
|
||||
# or
|
||||
# $ port install rb_msgpack # MacPorts
|
||||
#
|
||||
begin
|
||||
require 'rubygems'
|
||||
rescue LoadError
|
||||
end
|
||||
require 'msgpack'
|
||||
|
||||
def run(inio, outio)
|
||||
pac = MessagePack::Unpacker.new(inio)
|
||||
|
||||
begin
|
||||
pac.each {|obj|
|
||||
outio.write MessagePack.pack(obj)
|
||||
outio.flush
|
||||
}
|
||||
rescue EOFError
|
||||
return 0
|
||||
rescue
|
||||
$stderr.puts $!
|
||||
return 1
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
def usage
|
||||
puts <<EOF
|
||||
Usage: #{$0} [in-file] [out-file]
|
||||
|
||||
This tool is for testing of MessagePack implementation.
|
||||
This does following behavior:
|
||||
|
||||
1. Reads objects serialized by MessagePack from <in-file> (default: stdin)
|
||||
2. Re-serializes the objects using Ruby implementation of MessagePack (Note that Ruby implementation is considered valid)
|
||||
3. Writes the re-serialized objects into <out-file> (default: stdout)
|
||||
|
||||
EOF
|
||||
exit 1
|
||||
end
|
||||
|
||||
inio = $stdin
|
||||
outio = $stdout
|
||||
|
||||
if ARGV.length > 2
|
||||
usage
|
||||
end
|
||||
|
||||
ARGV.each {|str|
|
||||
if str.size > 1 && str[0] == ?-
|
||||
usage
|
||||
end
|
||||
}
|
||||
|
||||
if fname = ARGV[0]
|
||||
unless fname == "-"
|
||||
begin
|
||||
inio = File.open(fname)
|
||||
rescue
|
||||
puts "can't open output file: #{$!}"
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if fname = ARGV[1]
|
||||
unless fname == "-"
|
||||
begin
|
||||
outio = File.open(fname, "w")
|
||||
rescue
|
||||
puts "can't open output file: #{$!}"
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
code = run(inio, outio)
|
||||
|
||||
inio.close
|
||||
outio.close
|
||||
|
||||
exit code
|
||||
|
Reference in New Issue
Block a user