2014-01-19 14:42:02 +00:00
/*
This file is part of cpp - ethereum .
cpp - ethereum is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2014-01-31 21:08:16 +00:00
the Free Software Foundation , either version 2 of the License , or
2014-01-19 14:42:02 +00:00
( at your option ) any later version .
Foobar is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with Foobar . If not , see < http : //www.gnu.org/licenses/>.
*/
/** @file rlp.cpp
* @ author Gav Wood < i @ gavwood . com >
* @ date 2014
* RLP test functions .
*/
# include <RLP.h>
using namespace std ;
using namespace eth ;
int rlpTest ( )
{
// int of value 15
assert ( RLP ( " \x0f " ) = = 15 ) ;
assert ( asString ( rlp ( 15 ) ) = = " \x0f " ) ;
// 3-character string
2014-02-04 17:55:54 +00:00
assert ( RLP ( " \x83 " " dog " ) = = " dog " ) ;
assert ( asString ( rlp ( " dog " ) ) = = " \x83 " " dog " ) ;
2014-01-19 14:42:02 +00:00
// 2-item list
2014-02-05 01:16:11 +00:00
string twoItemListString = " \xc5 \x0f \x83 " " dog " ;
2014-01-19 14:42:02 +00:00
RLP twoItemList ( twoItemListString ) ;
assert ( twoItemList . itemCount ( ) = = 2 ) ;
assert ( twoItemList [ 0 ] = = 15 ) ;
assert ( twoItemList [ 1 ] = = " dog " ) ;
2014-02-05 01:16:11 +00:00
assert ( asString ( rlpList ( 15 , " dog " ) ) = = " \xc5 \x0f \x83 " " dog " ) ;
2014-02-04 17:55:54 +00:00
// null
assert ( RLP ( " \x80 " ) = = " " ) ;
assert ( asString ( rlp ( " " ) ) = = " \x80 " ) ;
2014-01-19 14:42:02 +00:00
// 1-byte (8-bit) int
2014-02-04 17:55:54 +00:00
assert ( RLP ( " \x81 \x80 " ) = = 128 ) ;
assert ( asString ( rlp ( 128 ) ) = = " \x81 \x80 " ) ;
2014-01-19 14:42:02 +00:00
// 2-byte (16-bit) int
2014-02-04 17:55:54 +00:00
assert ( RLP ( " \x82 \x01 \x01 " ) = = 257 ) ;
assert ( asString ( rlp ( 257 ) ) = = " \x82 \x01 \x01 " ) ;
2014-01-19 14:42:02 +00:00
// 32-byte (256-bit) int
2014-02-04 17:55:54 +00:00
assert ( RLP ( " \xa0 \x10 \x01 \x02 \x03 \x04 \x05 \x06 \x07 \x08 \x09 \x0a \x0b \x0c \x0d \x0e \x0f \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17 \x18 \x19 \x1a \x1b \x1c \x1d \x1e \x1f " ) = = bigint ( " 0x100102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f " ) ) ;
2014-02-05 01:16:11 +00:00
assert ( asString ( rlp ( bigint ( " 0x100102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f " ) ) ) = = " \xa0 \x10 \x01 \x02 \x03 \x04 \x05 \x06 \x07 \x08 \x09 \x0a \x0b \x0c \x0d \x0e \x0f \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17 \x18 \x19 \x1a \x1b \x1c \x1d \x1e \x1f " ) ;
2014-01-19 14:42:02 +00:00
// 56-character string.
2014-02-04 17:55:54 +00:00
assert ( RLP ( " \xb8 \x38 " " Lorem ipsum dolor sit amet, consectetur adipisicing elit " ) = = " Lorem ipsum dolor sit amet, consectetur adipisicing elit " ) ;
assert ( asString ( rlp ( " Lorem ipsum dolor sit amet, consectetur adipisicing elit " ) ) = = " \xb8 \x38 " " Lorem ipsum dolor sit amet, consectetur adipisicing elit " ) ;
2014-01-19 14:42:02 +00:00
return 0 ;
}