Pol  Revision:4b29d2b
testmisc.cpp
Go to the documentation of this file.
1 
6 #include <algorithm>
7 #include <array>
8 #include <cstring>
9 #include <string>
10 
11 #include "../../clib/logfacility.h"
12 #include "../../clib/rawtypes.h"
13 #include "../../plib/maptile.h"
14 #include "../dynproperties.h"
15 #include "../globals/uvars.h"
16 #include "../network/packethelper.h"
17 #include "../realms/realm.h"
18 
19 namespace Pol
20 {
21 namespace Testing
22 {
23 void dummy() {}
24 
25 void map_test()
26 {
28  INFO_PRINT << cell.landtile << " " << cell.z << "\n";
29 }
30 
32 {
33  class Test : public Core::DynamicPropsHolder
34  {
35  public:
36  DYN_PROPERTY( armod, s16, Core::PROP_AR_MOD, 0 );
37  DYN_PROPERTY( max_items, u32, Core::PROP_MAX_ITEMS_MOD, 0 );
38  DYN_PROPERTY( itemname, std::string, Core::PROP_NAME_SUFFIX, "" );
39  };
40  Test h;
41  INFO_PRINT << "size " << h.estimateSizeDynProps() << "\n";
42  INFO_PRINT << "ar " << h.armod() << " " << h.has_armod() << "\n";
43  h.armod( 10 );
44  INFO_PRINT << "ar " << h.armod() << " " << h.has_armod() << "\n";
45  h.armod( 0 );
46  INFO_PRINT << "ar " << h.armod() << " " << h.has_armod() << "\n";
47  INFO_PRINT << "size " << h.estimateSizeDynProps() << "\n";
48  INFO_PRINT << "name " << h.itemname() << " " << h.has_itemname() << "\n";
49  h.itemname( "hello world" );
50  INFO_PRINT << "name " << h.itemname() << " " << h.has_itemname() << "\n";
51  h.itemname( "" );
52  INFO_PRINT << "name " << h.itemname() << " " << h.has_itemname() << "\n";
53  INFO_PRINT << "size " << h.estimateSizeDynProps() << "\n";
54 }
55 
57 {
58  using namespace Network;
59  using namespace Network::PktHelper;
60  auto debug = []( const PacketOut<PktOut_2F>& p ) {
61  fmt::Writer w;
62  for ( const u8& c : p->buffer )
63  {
64  w << fmt::hex( c ) << " ";
65  }
66  INFO_PRINT << w.str() << "\n";
67  };
68  auto test = []( const PacketOut<PktOut_2F>& p, const std::array<s8, 10>& a ) {
69 
70  if ( std::equal( std::begin( p->buffer ), std::end( p->buffer ), std::begin( a ) ) )
71  INFO_PRINT << "success\n";
72  else
73  {
74  INFO_PRINT << "failed\n";
75 
76  fmt::Writer w;
77  for ( const u8& c : a )
78  {
79  w << fmt::hex( c ) << " ";
80  }
81  INFO_PRINT << w.str() << "\n";
82  }
83  };
84  {
85  PacketOut<PktOut_2F> p; // size 10
86  p->Write<s8>( 0x12 );
87  p->Write<u8>( 0x21u );
88  p->WriteFlipped<s8>( 0x12 );
89  p->WriteFlipped<u8>( 0x21u );
90  p->Write<u8>( 0u );
91  debug( p );
92  std::array<s8, 10> a{{0x2f, 0x12, 0x21, 0x12, 0x21, 0, 0, 0, 0, 0}};
93  test( p, a );
94  }
95  {
96  PacketOut<PktOut_2F> p; // size 10
97  p->Write<s16>( 0x1234 );
98  p->Write<u16>( 0x4321u );
99  p->WriteFlipped<s16>( 0x1234 );
100  p->WriteFlipped<u16>( 0x4321u );
101  p->Write<u8>( 0u );
102  debug( p );
103  std::array<s8, 10> a{{0x2f, 0x34, 0x12, 0x21, 0x43, 0x12, 0x34, 0x43, 0x21, 0}};
104  test( p, a );
105  }
106  {
107  PacketOut<PktOut_2F> p; // size 10
108  p->Write<s32>( 0x12344321 );
109  p->Write<u32>( 0x12344321u );
110  p->Write<u8>( 0u );
111  debug( p );
112  std::array<s8, 10> a{{0x2f, 0x21, 0x43, 0x34, 0x12, 0x21, 0x43, 0x34, 0x12, 0}};
113  test( p, a );
114  }
115  {
116  PacketOut<PktOut_2F> p; // size 10
117  p->WriteFlipped<s32>( 0x12344321 );
118  p->WriteFlipped<u32>( 0x12344321u );
119  p->Write<u8>( 0u );
120  debug( p );
121  std::array<s8, 10> a{{0x2f, 0x12, 0x34, 0x43, 0x21, 0x12, 0x34, 0x43, 0x21, 0}};
122  test( p, a );
123  }
124  {
125  PacketOut<PktOut_2F> p; // size 10
126  std::string s( "1234" );
127  p->Write( s.c_str(), 4, false );
128  u8 b[] = {0x12, 0x34, 0x43, 0x21};
129  p->Write( b, 4 );
130  p->Write<u8>( 0u );
131  debug( p );
132  std::array<s8, 10> a{{0x2f, 0x31, 0x32, 0x33, 0x34, 0x12, 0x34, 0x43, 0x21, 0}};
133  test( p, a );
134  }
135  {
136  PacketOut<PktOut_2F> p; // size 10
137  u16 b[] = {0x12, 0x34};
138  p->Write( &b[0], 2, false );
139  p->WriteFlipped( &b[0], 2, false );
140  p->Write<u8>( 0u );
141  debug( p );
142  std::array<s8, 10> a{{0x2f, 0x12, 0, 0x34, 0, 0, 0x12, 0, 0x34, 0}};
143  test( p, a );
144  u8* data = reinterpret_cast<u8*>( &( p->buffer[1] ) );
145  u8 d;
146  std::memcpy( &d, &( p->buffer[1] ), 1 );
147  INFO_PRINT << ( *data ) << " " << d << "\n";
148 
149  u32* data1 = reinterpret_cast<u32*>( &( p->buffer[1] ) );
150  u32 d1;
151  std::memcpy( &d1, &( p->buffer[1] ), 4 );
152  INFO_PRINT << ( *data1 ) << " " << d1 << "\n";
153  }
154 }
155 
156 } // namespace Testing
157 } // namespace Pol
unsigned short landtile
Definition: maptile.h:21
unsigned char u8
Definition: rawtypes.h:25
unsigned short u16
Definition: rawtypes.h:26
void packet_test()
Definition: testmisc.cpp:56
unsigned int u32
Definition: rawtypes.h:27
signed short s16
Definition: rawtypes.h:30
signed int s32
Definition: rawtypes.h:31
void dummy()
Definition: testmisc.cpp:23
signed char s8
Definition: rawtypes.h:29
GameState gamestate
Definition: uvars.cpp:74
std::unordered_map< u64, ScriptDiffData > data
Definition: osmod.cpp:966
#define DYN_PROPERTY(name, type, id, defaultvalue)
Definition: dynproperties.h:62
void dynprops_test()
Definition: testmisc.cpp:31
void map_test()
Definition: testmisc.cpp:25
#define INFO_PRINT
Definition: logfacility.h:223
Plib::MAPTILE_CELL getmaptile(unsigned short x, unsigned short y) const
Definition: realmfunc.cpp:772
Realms::Realm * main_realm
Definition: uvars.h:162
Definition: berror.cpp:12