1
1
package com .st1vms .android .jdbits ;
2
2
3
- import android .content . Context ;
3
+ import android .util . Log ;
4
4
5
- import androidx .test .platform .app .InstrumentationRegistry ;
6
5
import androidx .test .ext .junit .runners .AndroidJUnit4 ;
7
6
8
7
import org .junit .Test ;
9
8
import org .junit .runner .RunWith ;
10
9
11
- import static org .junit .Assert .*;
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 .DoubleType ;
16
+ import com .st1vms .android .jdbits .dtypes .Int16Type ;
17
+ import com .st1vms .android .jdbits .dtypes .Int32Type ;
18
+ import com .st1vms .android .jdbits .dtypes .Int64Type ;
19
+ import com .st1vms .android .jdbits .dtypes .Int8Type ;
20
+ import com .st1vms .android .jdbits .dtypes .UInt16Type ;
21
+ import com .st1vms .android .jdbits .dtypes .UInt32Type ;
22
+ import com .st1vms .android .jdbits .dtypes .UInt64Type ;
23
+ import com .st1vms .android .jdbits .dtypes .UInt8Type ;
24
+ import com .st1vms .android .jdbits .dtypes .UTF8StringType ;
25
+
26
+ import java .io .UnsupportedEncodingException ;
27
+ import java .util .ArrayList ;
28
+ import java .util .Locale ;
12
29
13
30
/**
14
31
* Instrumented test, which will execute on an Android device.
18
35
@ RunWith (AndroidJUnit4 .class )
19
36
public class ExampleInstrumentedTest {
20
37
@ Test
21
- public void useAppContext () {
38
+ public void useAppContext () throws UnsupportedEncodingException {
22
39
// Context of the app under test.
23
- Context appContext = InstrumentationRegistry .getInstrumentation ().getTargetContext ();
24
- assertEquals ("com.st1vms.android.jdbits.test" , appContext .getPackageName ());
40
+ DTypes [] template = new DTypes []{
41
+ DTypes .UINT8_TYPE ,
42
+ DTypes .UINT16_TYPE ,
43
+ DTypes .UINT32_TYPE ,
44
+ DTypes .UINT64_TYPE ,
45
+ DTypes .INT8_TYPE ,
46
+ DTypes .INT16_TYPE ,
47
+ DTypes .INT32_TYPE ,
48
+ DTypes .INT64_TYPE ,
49
+ DTypes .BOOLEAN_TYPE ,
50
+ DTypes .DOUBLE_TYPE ,
51
+ DTypes .UTF8_STRING_TYPE
52
+ };
53
+
54
+ if (!DBits .RegisterPacket (0 , template )){
55
+ return ;
56
+ }
57
+
58
+
59
+ DPacket packet = new DPacket (0 , new ArrayList <>(){
60
+ {
61
+ add (new UInt8Type ((short ) 42 ));
62
+ add (new UInt16Type ((short ) 160 ));
63
+ add (new UInt32Type (4002 ));
64
+ add (new UInt64Type (41812 ));
65
+ add (new Int8Type ((byte ) -42 ));
66
+ add (new Int16Type ((short ) -160 ));
67
+ add (new Int32Type (-4002 ));
68
+ add (new Int64Type (-41812 ));
69
+ add (new BooleanType (false ));
70
+ add (new DoubleType (160.051 ));
71
+ add (new UTF8StringType ("I do desire we may be better strangers" ));
72
+ }
73
+ });
74
+
75
+ final byte [] buf = DBits .SerializeDPacket (packet );
76
+ if (buf != null ){
77
+
78
+ StringBuilder s = new StringBuilder ();
79
+ for (byte b : buf ){
80
+ s .append (String .format (Locale .getDefault (), "0x%02x " , b ));
81
+ }
82
+ Log .d ("TAG" ,String .format (Locale .getDefault (),
83
+ "\n Serialized to %d bytes: %s" , buf .length , s ));
84
+
85
+ try {
86
+
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
+ }
25
132
}
26
133
}
0 commit comments