-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.php
137 lines (121 loc) · 4.44 KB
/
test.php
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
<?php
/**
* Use tests in folder tests
*/
require_once("./lib/PhpBuf.php");
require_once("./tests/Example.php");
require_once("./tests/ExampleRepeat.php");
require_once("./tests/Example/Test1.php");
require_once("./tests/Example/Test3.php");
echo "Test values less than 128 (encode/decode)\n";
$result = PhpBuf_Base128::decodeFromReader(new PhpBuf_IO_Reader(PhpBuf_Base128::encode(120)));
echo "120 == " . $result ."\n\n";
echo "Test values more than 128 (only decode)\n";
$result = PhpBuf_Base128::decodeFromReader(new PhpBuf_IO_Reader(pack("n", 44034)));
echo "300 == " . $result ."\n\n";
echo "Test values more than 128 (encode/decode)\n";
$encode = PhpBuf_Base128::encode(300);
$result = PhpBuf_Base128::decodeFromReader(new PhpBuf_IO_Reader($encode));
echo "encoded: $encode\n\n";
echo "300 == " . $result ."\n\n";
echo "Test values more than 128 (encode/decode)\n";
$encode = /*pack("n", 8891723);*/PhpBuf_Base128::encode(123456789);
$result = PhpBuf_Base128::decodeFromReader(new PhpBuf_IO_Reader($encode));
echo "encoded: $encode\n\n";
echo "123456789 == " . $result ."\n\n";
echo "Test ZigZag class\n";
echo "value: 0\n";
$encode = PhpBuf_ZigZag::encode(0);
echo "encoded: ". $encode ."\n";
echo "decoded: " . PhpBuf_ZigZag::decode($encode) . "\n\n";
echo "value: 1\n";
$encode = PhpBuf_ZigZag::encode(1);
echo "encoded: ". $encode ."\n";
echo "decoded: " . PhpBuf_ZigZag::decode($encode) . "\n\n";
echo "value: -1\n";
$encode = PhpBuf_ZigZag::encode(-1);
echo "encoded: ". $encode ."\n";
echo "decoded: " . PhpBuf_ZigZag::decode($encode) . "\n\n";
echo "value: 123456789\n";
$encode = PhpBuf_ZigZag::encode(123456789);
echo "encoded: ". $encode ."\n";
echo "decoded: " . PhpBuf_ZigZag::decode($encode) . "\n\n";
echo "value: -123456789\n";
$encode = PhpBuf_ZigZag::encode(-123456789);
echo "encoded: ". $encode ."\n";
echo "decoded: " . PhpBuf_ZigZag::decode($encode) . "\n\n";
echo "Test Reader & Writer\n";
$writer = new PhpBuf_IO_Writer();
$writer->writeBytes("test1");
echo "lenght: ". $writer->getLenght() ."\n";
echo "test1 = ". $writer->getData() ."\n";
$writer->writeBytes("test2");
echo "lenght: ". $writer->getLenght() ."\n";
echo "test1test2 = ". $writer->getData() ."\n";
$writer->redo();
echo "lenght: ". $writer->getLenght() ."\n";
echo "test1 = ". $writer->getData() ."\n";
$writer->writeByte("!");
echo "lenght: ". $writer->getLenght() ."\n";
echo "test1! = ". $writer->getData() ."\n";
$writer->redo();
echo "lenght: ". $writer->getLenght() ."\n";
echo "test1 = ". $writer->getData() ."\n";
$writer->writeBytes(" StringForReaders");
echo "lenght: ". $writer->getLenght() ."\n";
echo "test1 StringForReaders = ". $writer->getData() ."\n";
$reader = PhpBuf_IO_Reader::createFromWriter($writer);
echo "t = ". $reader->getByte() ."\n";
echo "position: " .$reader->getPosition() . "\n";
echo "e = ". $reader->getByte() ."\n";
echo "position: " .$reader->getPosition() . "\n";
echo "st1 = ". $reader->getBytes(3) ."\n";
echo "position: " .$reader->getPosition() . "\n";
$reader->setPosition(12);
echo "For = ". $reader->getBytes(3) ."\n";
echo "position: " .$reader->getPosition() . "\n";
$reader->redo();
echo "For = ". $reader->getBytes(3) ."\n";
echo "position: " .$reader->getPosition() . "\n";
echo "Test Messages\n";
$message = new PhpBuf_Message_Example();
$writer = new PhpBuf_IO_Writer();
$message->id = 150;
$message->balance = -12345;
$message->isAdmin = true;
$message->status = "deleted";
$message->name = "Andrey Lepeshkin";
$message->write($writer);
echo $writer->getData() . "\n";
foreach (str_split($writer->getData()) as $byte) {
echo ord($byte) . "\n";
}
$reader = PhpBuf_IO_Reader::createFromWriter($writer);
$message = new PhpBuf_Message_Example();
$message->read($reader);
echo "id: " . $message->id . "\n";
echo "balance: " . $message->balance . "\n";
if($message->isAdmin === true) {
echo "isAdmin: true\n";
}
echo "status: " . $message->status . "\n";
echo "name: " . $message->name . "\n";
$messagesArray = array();
$main = new PhpBuf_Message_ExampleRepeat();
$writer = new PhpBuf_IO_Writer();
for ($i = 0; $i < 5; $i++) {
$nested = new PhpBuf_Message_Example();
$nested->id = $i;
$nested->balance = -12345 + ($i * 10);
$nested->isAdmin = false;
$nested->status = "deleted";
$nested->name = "name for $i";
$nested->bytes = "Some bytes for $i";
$messagesArray[] = $nested;
}
$main->messages = $messagesArray;
$main->write($writer);
$reader = PhpBuf_IO_Reader::createFromWriter($writer);
$main = new PhpBuf_Message_ExampleRepeat();
$main->read($reader);
print_r($main);