1
+ package com .st1vms .android .jdbits .app ;
2
+
3
+ import android .util .Log ;
4
+
5
+ import androidx .test .ext .junit .runners .AndroidJUnit4 ;
6
+
7
+ import org .junit .Test ;
8
+ import org .junit .runner .RunWith ;
9
+
10
+
11
+ import com .st1vms .android .jdbits .DBits ;
12
+ import com .st1vms .android .jdbits .DPacket ;
13
+ import com .st1vms .android .jdbits .DTypes ;
14
+ import com .st1vms .android .jdbits .dtypes .BooleanType ;
15
+ import com .st1vms .android .jdbits .dtypes .DSerializable ;
16
+ import com .st1vms .android .jdbits .dtypes .DoubleType ;
17
+ import com .st1vms .android .jdbits .dtypes .Int16Type ;
18
+ import com .st1vms .android .jdbits .dtypes .Int32Type ;
19
+ import com .st1vms .android .jdbits .dtypes .Int64Type ;
20
+ import com .st1vms .android .jdbits .dtypes .Int8Type ;
21
+ import com .st1vms .android .jdbits .dtypes .UInt16Type ;
22
+ import com .st1vms .android .jdbits .dtypes .UInt32Type ;
23
+ import com .st1vms .android .jdbits .dtypes .UInt64Type ;
24
+ import com .st1vms .android .jdbits .dtypes .UInt8Type ;
25
+ import com .st1vms .android .jdbits .dtypes .UTF8StringType ;
26
+
27
+ import java .io .UnsupportedEncodingException ;
28
+ import java .util .ArrayList ;
29
+ import java .util .Locale ;
30
+
31
+ /**
32
+ * Instrumented test, which will execute on an Android device.
33
+ *
34
+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
35
+ */
36
+ @ RunWith (AndroidJUnit4 .class )
37
+ public class ExampleInstrumentedTest {
38
+ @ Test
39
+ public void useAppContext () throws UnsupportedEncodingException {
40
+ // Context of the app under test.
41
+ DTypes [] template = new DTypes []{
42
+ DTypes .UINT8_TYPE ,
43
+ DTypes .UINT16_TYPE ,
44
+ DTypes .UINT32_TYPE ,
45
+ DTypes .UINT64_TYPE ,
46
+ DTypes .INT8_TYPE ,
47
+ DTypes .INT16_TYPE ,
48
+ DTypes .INT32_TYPE ,
49
+ DTypes .INT64_TYPE ,
50
+ DTypes .BOOLEAN_TYPE ,
51
+ DTypes .DOUBLE_TYPE ,
52
+ DTypes .UTF8_STRING_TYPE
53
+ };
54
+
55
+ if (!DBits .RegisterPacket (0 , template )){
56
+ return ;
57
+ }
58
+
59
+
60
+ DPacket packet = new DPacket (0 , new ArrayList <>(){
61
+ {
62
+ add (new UInt8Type ((short ) 42 ));
63
+ add (new UInt16Type ((short ) 160 ));
64
+ add (new UInt32Type (4002 ));
65
+ add (new UInt64Type (41812 ));
66
+ add (new Int8Type ((byte ) -42 ));
67
+ add (new Int16Type ((short ) -160 ));
68
+ add (new Int32Type (-4002 ));
69
+ add (new Int64Type (-41812 ));
70
+ add (new BooleanType (false ));
71
+ add (new DoubleType (160.051 ));
72
+ add (new UTF8StringType ("I do desire we may be better strangers" ));
73
+ }
74
+ });
75
+
76
+ final byte [] buf = DBits .SerializeDPacket (packet );
77
+ if (buf != null ){
78
+
79
+ StringBuilder s = new StringBuilder ();
80
+ for (byte b : buf ){
81
+ s .append (String .format (Locale .getDefault (), "0x%02x " , b ));
82
+ }
83
+ Log .d ("TAG" ,String .format (Locale .getDefault (),
84
+ "\n Serialized to %d bytes: %s" , buf .length , s ));
85
+
86
+ try {
87
+ DPacket p = DBits .DeserializeBuffer (buf , buf .length );
88
+ if (p == null ){
89
+ Log .d ("TAG" ,"Deserialize Error!" );
90
+ return ;
91
+ }
92
+ UInt8Type u8 = (UInt8Type ) p .getField (0 );
93
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "U8 -> %d" ,u8 .getShortValue ()));
94
+
95
+ UInt16Type u16 = (UInt16Type ) p .getField (1 );
96
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "U16 -> %d" ,u16 .getIntValue ()));
97
+
98
+ UInt32Type u32 = (UInt32Type ) p .getField (2 );
99
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "U32 -> %d" ,u32 .getIntValue ()));
100
+
101
+ UInt64Type u64 = (UInt64Type ) p .getField (3 );
102
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "U64 -> %d" ,u64 .getLongValue ()));
103
+
104
+ Int8Type i8 = (Int8Type ) p .getField (4 );
105
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "I8 -> %d" ,i8 .getByteValue ()));
106
+
107
+ Int16Type i16 = (Int16Type ) p .getField (5 );
108
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "I16 -> %d" ,i16 .getShortValue ()));
109
+
110
+ Int32Type i32 = (Int32Type ) p .getField (6 );
111
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "I32 -> %d" ,i32 .getIntValue ()));
112
+
113
+ Int64Type i64 = (Int64Type ) p .getField (7 );
114
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "I64 -> %d" ,i64 .getLongValue ()));
115
+
116
+ BooleanType b = (BooleanType ) p .getField (8 );
117
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "B -> %b" ,b .getBoolValue ()));
118
+
119
+ DoubleType d = (DoubleType ) p .getField (9 );
120
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "D -> %f" ,d .getDoubleValue ()));
121
+
122
+ UTF8StringType str = (UTF8StringType ) p .getField (10 );
123
+ Log .d ("TAG" ,String .format (Locale .getDefault (), "UTF8 -> %s" ,str .getUTF8StringValue ()));
124
+
125
+ }catch (Exception e ){
126
+ e .printStackTrace ();
127
+ }
128
+
129
+ }else {
130
+ Log .d ("TAG" ,"Buffer is null" );
131
+ }
132
+ }
133
+ }
0 commit comments