bytebuffer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/src/lib.rs ADDED
@@ -0,0 +1,739 @@
1
+ use std::ffi::{CStr, CString};
2
+ use std::os::raw::c_char;
3
+ use bytebuffer::{ByteBuffer, Endian};
4
+
5
+ #[no_mangle]
6
+ extern "C" fn new() -> *mut ByteBuffer {
7
+ Box::into_raw(Box::new(ByteBuffer::new()))
8
+ }
9
+
10
+ #[no_mangle]
11
+ extern "C" fn from_bytes(bytes: *const u8, len: usize) -> *mut ByteBuffer {
12
+ let slice = unsafe {
13
+ assert!(!bytes.is_null());
14
+ std::slice::from_raw_parts(bytes, len)
15
+ };
16
+ Box::into_raw(Box::new(ByteBuffer::from_bytes(slice)))
17
+ }
18
+
19
+ #[no_mangle]
20
+ extern "C" fn drop(ptr: *mut ByteBuffer) {
21
+ let _ = unsafe { Box::from_raw(ptr) };
22
+ }
23
+
24
+ #[no_mangle]
25
+ extern "C" fn length(ptr: *mut ByteBuffer) -> usize {
26
+ match unsafe { raw_pointer_to_ref(ptr) } {
27
+ Some(buffer) => buffer.len(),
28
+ None => panic!("Unable to locate buffer using ptr")
29
+ }
30
+ }
31
+
32
+ #[no_mangle]
33
+ extern "C" fn is_empty(ptr: *mut ByteBuffer) -> bool {
34
+ match unsafe { raw_pointer_to_ref(ptr) } {
35
+ Some(buffer) => buffer.is_empty(),
36
+ None => panic!("Unable to locate buffer using ptr")
37
+ }
38
+ }
39
+
40
+ #[no_mangle]
41
+ extern "C" fn clear(ptr: *mut ByteBuffer) {
42
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
43
+ Some(buffer) => buffer.clear(),
44
+ None => panic!("Unable to locate buffer using ptr")
45
+ }
46
+ }
47
+
48
+ #[no_mangle]
49
+ extern "C" fn reset_cursors(ptr: *mut ByteBuffer) {
50
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
51
+ Some(buffer) => buffer.reset_cursors(),
52
+ None => panic!("Unable to locate buffer using ptr")
53
+ }
54
+ }
55
+
56
+ #[no_mangle]
57
+ extern "C" fn reset_bits_cursor(ptr: *mut ByteBuffer) {
58
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
59
+ Some(buffer) => buffer.reset_bits_cursors(),
60
+ None => panic!("Unable to locate buffer using ptr")
61
+ }
62
+ }
63
+
64
+ #[no_mangle]
65
+ extern "C" fn resize(ptr: *mut ByteBuffer, size: usize) {
66
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
67
+ Some(buffer) => buffer.resize(size),
68
+ None => panic!("Unable to locate buffer using ptr")
69
+ }
70
+ }
71
+
72
+ #[no_mangle]
73
+ extern "C" fn flush_bits(ptr: *mut ByteBuffer) {
74
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
75
+ Some(buffer) => buffer.flush_bits(),
76
+ None => panic!("Unable to locate buffer using ptr")
77
+ }
78
+ }
79
+
80
+ #[no_mangle]
81
+ extern "C" fn get_read_position(ptr: *mut ByteBuffer) -> usize {
82
+ match unsafe { raw_pointer_to_ref(ptr) } {
83
+ Some(buffer) => buffer.get_rpos(),
84
+ None => panic!("Unable to locate buffer using ptr")
85
+ }
86
+ }
87
+
88
+ #[no_mangle]
89
+ extern "C" fn get_write_position(ptr: *mut ByteBuffer) -> usize {
90
+ match unsafe { raw_pointer_to_ref(ptr) } {
91
+ Some(buffer) => buffer.get_wpos(),
92
+ None => panic!("Unable to locate buffer using ptr")
93
+ }
94
+ }
95
+
96
+ #[no_mangle]
97
+ extern "C" fn set_write_position(ptr: *mut ByteBuffer, position: usize) {
98
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
99
+ Some(buffer) => buffer.set_wpos(position),
100
+ None => panic!("Unable to locate buffer using ptr")
101
+ }
102
+ }
103
+
104
+ #[no_mangle]
105
+ extern "C" fn set_read_position(ptr: *mut ByteBuffer, position: usize) {
106
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
107
+ Some(buffer) => buffer.set_rpos(position),
108
+ None => panic!("Unable to locate buffer using ptr")
109
+ }
110
+ }
111
+
112
+ #[no_mangle]
113
+ extern "C" fn set_endian(ptr: *mut ByteBuffer, endian: u8) {
114
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
115
+ Some(buffer) => {
116
+ let endian: Endian = endian_for_u8(endian);
117
+ buffer.set_endian(endian);
118
+ },
119
+ None => panic!("Unable to locate buffer using ptr")
120
+ }
121
+ }
122
+
123
+ #[no_mangle]
124
+ extern "C" fn get_endian(ptr: *mut ByteBuffer) -> u8 {
125
+ match unsafe { raw_pointer_to_ref(ptr) } {
126
+ Some(buffer) => {
127
+ let endian: Endian = buffer.endian();
128
+ u8_for_endian(endian)
129
+ },
130
+ None => panic!("Unable to locate buffer using ptr")
131
+ }
132
+ }
133
+
134
+ #[no_mangle]
135
+ extern "C" fn write_bit(ptr: *mut ByteBuffer, bit: bool) {
136
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
137
+ Some(buffer) => buffer.write_bit(bit),
138
+ None => panic!("Unable to locate buffer using ptr")
139
+ }
140
+ }
141
+
142
+ #[no_mangle]
143
+ extern "C" fn write_bits(ptr: *mut ByteBuffer, value: u64, bits: u8) {
144
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
145
+ Some(buffer) => buffer.write_bits(value, bits),
146
+ None => panic!("Unable to locate buffer using ptr")
147
+ }
148
+ }
149
+
150
+ #[no_mangle]
151
+ extern "C" fn write_u8(ptr: *mut ByteBuffer, val: u8) {
152
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
153
+ Some(buffer) => buffer.write_u8(val),
154
+ None => panic!("Unable to locate buffer using ptr")
155
+ }
156
+ }
157
+
158
+ #[no_mangle]
159
+ extern "C" fn write_i8(ptr: *mut ByteBuffer, val: i8) {
160
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
161
+ Some(buffer) => buffer.write_i8(val),
162
+ None => panic!("Unable to locate buffer using ptr")
163
+ }
164
+ }
165
+
166
+ #[no_mangle]
167
+ extern "C" fn write_u16(ptr: *mut ByteBuffer, val: u16) {
168
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
169
+ Some(buffer) => buffer.write_u16(val),
170
+ None => panic!("Unable to locate buffer using ptr")
171
+ }
172
+ }
173
+
174
+ #[no_mangle]
175
+ extern "C" fn write_i16(ptr: *mut ByteBuffer, val: i16) {
176
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
177
+ Some(buffer) => buffer.write_i16(val),
178
+ None => panic!("Unable to locate buffer using ptr")
179
+ }
180
+ }
181
+
182
+ #[no_mangle]
183
+ extern "C" fn write_u32(ptr: *mut ByteBuffer, val: u32) {
184
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
185
+ Some(buffer) => buffer.write_u32(val),
186
+ None => panic!("Unable to locate buffer using ptr")
187
+ }
188
+ }
189
+
190
+ #[no_mangle]
191
+ extern "C" fn write_i32(ptr: *mut ByteBuffer, val: i32) {
192
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
193
+ Some(buffer) => buffer.write_i32(val),
194
+ None => panic!("Unable to locate buffer using ptr")
195
+ }
196
+ }
197
+
198
+ #[no_mangle]
199
+ extern "C" fn write_u64(ptr: *mut ByteBuffer, val: u64) {
200
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
201
+ Some(buffer) => buffer.write_u64(val),
202
+ None => panic!("Unable to locate buffer using ptr")
203
+ }
204
+ }
205
+
206
+ #[no_mangle]
207
+ extern "C" fn write_i64(ptr: *mut ByteBuffer, val: i64) {
208
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
209
+ Some(buffer) => buffer.write_i64(val),
210
+ None => panic!("Unable to locate buffer using ptr")
211
+ }
212
+ }
213
+
214
+ #[no_mangle]
215
+ extern "C" fn write_f32(ptr: *mut ByteBuffer, val: f32) {
216
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
217
+ Some(buffer) => buffer.write_f32(val),
218
+ None => panic!("Unable to locate buffer using ptr")
219
+ }
220
+ }
221
+
222
+ #[no_mangle]
223
+ extern "C" fn write_f64(ptr: *mut ByteBuffer, val: f64) {
224
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
225
+ Some(buffer) => buffer.write_f64(val),
226
+ None => panic!("Unable to locate buffer using ptr")
227
+ }
228
+ }
229
+
230
+ #[no_mangle]
231
+ extern "C" fn write_bytes(ptr: *mut ByteBuffer, bytes: *const u8, length: usize) {
232
+ let slice = unsafe {
233
+ assert!(!bytes.is_null());
234
+ std::slice::from_raw_parts(bytes, length)
235
+ };
236
+
237
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
238
+ Some(buffer) => buffer.write_bytes(slice),
239
+ None => panic!("Unable to locate buffer using ptr")
240
+ }
241
+ }
242
+
243
+ #[no_mangle]
244
+ extern "C" fn write_string(ptr: *mut ByteBuffer, c_string: *const c_char) {
245
+ let raw_str: &CStr = unsafe { CStr::from_ptr(c_string) };
246
+ let string: &str = raw_str.to_str().unwrap();
247
+
248
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
249
+ Some(buffer) => buffer.write_string(string),
250
+ None => panic!("Unable to locate buffer using ptr")
251
+ }
252
+ }
253
+
254
+ #[no_mangle]
255
+ extern "C" fn read_bits(ptr: *mut ByteBuffer, length: u8) -> u64 {
256
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
257
+ Some(buffer) => match buffer.read_bits(length) {
258
+ Ok(value) => value,
259
+ Err(error) => panic!("Unable to read bits from buffer: {}", error),
260
+ },
261
+ None => panic!("Unable to locate buffer using ptr")
262
+ }
263
+ }
264
+
265
+ #[no_mangle]
266
+ extern "C" fn read_bit(ptr: *mut ByteBuffer) -> bool {
267
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
268
+ Some(buffer) =>
269
+ match buffer.read_bit() {
270
+ Ok(bit) => bit,
271
+ Err(_) => panic!("Unable to read bit from buffer")
272
+ },
273
+ None => panic!("Unable to locate buffer using ptr")
274
+ }
275
+ }
276
+
277
+ #[no_mangle]
278
+ extern "C" fn read_u8(ptr: *mut ByteBuffer) -> u8 {
279
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
280
+ Some(buffer) => {
281
+ match buffer.read_u8() {
282
+ Ok(value) => value,
283
+ Err(_) => panic!("Unable to read u8 from buffer")
284
+ }
285
+ },
286
+ None => panic!("Unable to locate buffer using ptr")
287
+ }
288
+ }
289
+
290
+ #[no_mangle]
291
+ extern "C" fn read_i8(ptr: *mut ByteBuffer) -> i8 {
292
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
293
+ Some(buffer) => {
294
+ match buffer.read_i8() {
295
+ Ok(value) => value,
296
+ Err(_) => panic!("Unable to read i8 from buffer")
297
+ }
298
+ },
299
+ None => panic!("Unable to locate buffer using ptr")
300
+ }
301
+ }
302
+
303
+ #[no_mangle]
304
+ extern "C" fn read_u16(ptr: *mut ByteBuffer) -> u16 {
305
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
306
+ Some(buffer) => {
307
+ match buffer.read_u16() {
308
+ Ok(value) => value,
309
+ Err(_) => panic!("Unable to read u16 from buffer")
310
+ }
311
+ },
312
+ None => panic!("Unable to locate buffer using ptr")
313
+ }
314
+ }
315
+
316
+ #[no_mangle]
317
+ extern "C" fn read_i16(ptr: *mut ByteBuffer) -> i16 {
318
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
319
+ Some(buffer) => {
320
+ match buffer.read_i16() {
321
+ Ok(value) => value,
322
+ Err(_) => panic!("Unable to read i16 from buffer")
323
+ }
324
+ },
325
+ None => panic!("Unable to locate buffer using ptr")
326
+ }
327
+ }
328
+
329
+ #[no_mangle]
330
+ extern "C" fn read_u32(ptr: *mut ByteBuffer) -> u32 {
331
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
332
+ Some(buffer) => {
333
+ match buffer.read_u32() {
334
+ Ok(value) => value,
335
+ Err(_) => panic!("Unable to read u32 from buffer")
336
+ }
337
+ },
338
+ None => panic!("Unable to locate buffer using ptr")
339
+ }
340
+ }
341
+
342
+ #[no_mangle]
343
+ extern "C" fn read_i32(ptr: *mut ByteBuffer) -> i32 {
344
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
345
+ Some(buffer) => {
346
+ match buffer.read_i32() {
347
+ Ok(value) => value,
348
+ Err(_) => panic!("Unable to read i32 from buffer")
349
+ }
350
+ },
351
+ None => panic!("Unable to locate buffer using ptr")
352
+ }
353
+ }
354
+
355
+ #[no_mangle]
356
+ extern "C" fn read_u64(ptr: *mut ByteBuffer) -> u64 {
357
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
358
+ Some(buffer) => {
359
+ match buffer.read_u64() {
360
+ Ok(value) => value,
361
+ Err(_) => panic!("Unable to read u64 from buffer")
362
+ }
363
+ },
364
+ None => panic!("Unable to locate buffer using ptr")
365
+ }
366
+ }
367
+
368
+ #[no_mangle]
369
+ extern "C" fn read_i64(ptr: *mut ByteBuffer) -> i64 {
370
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
371
+ Some(buffer) => {
372
+ match buffer.read_i64() {
373
+ Ok(value) => value,
374
+ Err(_) => panic!("Unable to read i64 from buffer")
375
+ }
376
+ },
377
+ None => panic!("Unable to locate buffer using ptr")
378
+ }
379
+ }
380
+
381
+ #[no_mangle]
382
+ extern "C" fn read_f32(ptr: *mut ByteBuffer) -> f32 {
383
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
384
+ Some(buffer) => {
385
+ match buffer.read_f32() {
386
+ Ok(value) => value,
387
+ Err(_) => panic!("Unable to read f32 from buffer")
388
+ }
389
+ },
390
+ None => panic!("Unable to locate buffer using ptr")
391
+ }
392
+ }
393
+
394
+ #[no_mangle]
395
+ extern "C" fn read_f64(ptr: *mut ByteBuffer) -> f64 {
396
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
397
+ Some(buffer) => {
398
+ match buffer.read_f64() {
399
+ Ok(value) => value,
400
+ Err(_) => panic!("Unable to read f64 from buffer")
401
+ }
402
+ },
403
+ None => panic!("Unable to locate buffer using ptr")
404
+ }
405
+ }
406
+
407
+ #[no_mangle]
408
+ extern "C" fn read_string(ptr: *mut ByteBuffer) -> *const c_char {
409
+ match unsafe { raw_pointer_to_ref_mut(ptr) } {
410
+ Some(buffer) => {
411
+ let str = match buffer.read_string() {
412
+ Ok(value) => value,
413
+ Err(_) => panic!("Unable to read string from buffer")
414
+ };
415
+ let cstr = CString::new(str).unwrap();
416
+ cstr.into_raw()
417
+ },
418
+ None => panic!("Unable to locate buffer using ptr")
419
+ }
420
+ }
421
+
422
+ #[no_mangle]
423
+ extern "C" fn to_hex_string(ptr: *mut ByteBuffer) -> *const c_char {
424
+ match unsafe { raw_pointer_to_ref(ptr) } {
425
+ Some(buffer) => {
426
+ let dump: String = buffer.to_hex_dump();
427
+ let c_s = CString::new(dump).unwrap();
428
+ c_s.into_raw()
429
+ },
430
+ None => panic!("Unable to locate buffer using ptr")
431
+ }
432
+ }
433
+
434
+ unsafe fn raw_pointer_to_ref(buffer: *mut ByteBuffer) -> Option<&'static ByteBuffer> {
435
+ if buffer.is_null() {
436
+ None
437
+ } else {
438
+ Some(&*buffer)
439
+ }
440
+ }
441
+
442
+ unsafe fn raw_pointer_to_ref_mut(buffer: *mut ByteBuffer) -> Option<&'static mut ByteBuffer> {
443
+ buffer.as_mut()
444
+ }
445
+
446
+ fn endian_for_u8(value: u8) -> Endian {
447
+ match value {
448
+ 0 => Endian::BigEndian,
449
+ 1 => Endian::LittleEndian,
450
+ _ => unreachable!(),
451
+ }
452
+ }
453
+
454
+ fn u8_for_endian(endian: Endian) -> u8 {
455
+ match endian {
456
+ Endian::BigEndian => 0,
457
+ Endian::LittleEndian => 1
458
+ }
459
+ }
460
+
461
+ #[cfg(test)]
462
+ mod tests {
463
+ use super::*;
464
+
465
+ #[test]
466
+ fn test_new_bytebuffer() {
467
+ let buffer_ref = new();
468
+ assert!(!buffer_ref.is_null());
469
+ drop(buffer_ref);
470
+ }
471
+
472
+ #[test]
473
+ fn test_from_bytes() {
474
+ let bytes = [0, 1, 2, 3, 4, 5];
475
+ let buffer = from_bytes(bytes.as_ptr(), bytes.len());
476
+ assert!(!buffer.is_null(), "ByteBuffer from bytes failed.");
477
+ assert_eq!(length(buffer), bytes.len(), "Length of ByteBuffer mismatched with input bytes.");
478
+ drop(buffer);
479
+ }
480
+
481
+ #[test]
482
+ fn test_length() {
483
+ let bytes = [0, 1, 2, 3, 4, 5];
484
+ let buffer = from_bytes(bytes.as_ptr(), bytes.len());
485
+ assert_eq!(length(buffer), bytes.len(), "Length of ByteBuffer mismatched with input bytes.");
486
+ drop(buffer);
487
+ }
488
+
489
+ #[test]
490
+ fn test_is_empty() {
491
+ let empty_buffer = new();
492
+ assert_eq!(is_empty(empty_buffer), true, "New ByteBuffer expected to be empty.");
493
+ drop(empty_buffer);
494
+
495
+ let bytes = [0, 1, 2, 3, 4, 5];
496
+ let non_empty_buffer = from_bytes(bytes.as_ptr(), bytes.len());
497
+ assert_eq!(is_empty(non_empty_buffer), false, "ByteBuffer from bytes should not be empty.");
498
+ drop(non_empty_buffer);
499
+ }
500
+
501
+ #[test]
502
+ fn test_clear() {
503
+ let bytes = [0, 1, 2, 3, 4, 5];
504
+ let buffer = from_bytes(bytes.as_ptr(), bytes.len());
505
+ clear(buffer);
506
+ assert_eq!(is_empty(buffer), true, "Cleared ByteBuffer expected to be empty.");
507
+ drop(buffer);
508
+ }
509
+
510
+ #[test]
511
+ fn test_drop() {
512
+ let bytes = [0, 1, 2, 3, 4, 5];
513
+ let buffer = from_bytes(bytes.as_ptr(), bytes.len());
514
+ drop(buffer);
515
+ }
516
+
517
+ #[test]
518
+ fn test_set_endian() {
519
+ let buffer = new();
520
+ assert_eq!(get_endian(buffer), u8_for_endian(Endian::BigEndian));
521
+
522
+ set_endian(buffer, 1);
523
+ assert_eq!(get_endian(buffer), u8_for_endian(Endian::LittleEndian));
524
+ }
525
+
526
+ #[test]
527
+ fn test_get_endian() {
528
+ let buffer = new();
529
+ assert_eq!(get_endian(buffer), u8_for_endian(Endian::BigEndian))
530
+ }
531
+
532
+ #[test]
533
+ fn test_write_u8() {
534
+ let bytes = [0, 1, 2, 3, 4, 5];
535
+ let buffer = from_bytes(bytes.as_ptr(), bytes.len());
536
+ write_u8(buffer, 0);
537
+ assert_eq!(length(buffer), 7, "ByteBuffer expected to have 1 byte written.");
538
+ }
539
+
540
+ #[test]
541
+ fn test_write_u16() {
542
+ let buffer = new();
543
+ write_u16(buffer, 808);
544
+ assert_eq!(length(buffer), 2, "ByteBuffer expected to have 2 bytes written.");
545
+ }
546
+
547
+ #[test]
548
+ fn test_write_u32() {
549
+ let buffer = new();
550
+ write_u32(buffer, 65580);
551
+ assert_eq!(length(buffer), 4, "ByteBuffer expected to have 4 bytes written.");
552
+ }
553
+
554
+ #[test]
555
+ fn test_write_u64() {
556
+ let buffer = new();
557
+ write_u64(buffer, 2147888888);
558
+ assert_eq!(length(buffer), 8, "ByteBuffer expected to have 8 bytes written.");
559
+ }
560
+
561
+ #[test]
562
+ fn test_write_i8() {
563
+ let buffer = new();
564
+ write_i8(buffer, -48);
565
+ assert_eq!(length(buffer), 1, "ByteBuffer expected to have 1 byte written.");
566
+ }
567
+
568
+ #[test]
569
+ fn test_write_i16() {
570
+ let buffer = new();
571
+ write_i16(buffer, -808);
572
+ assert_eq!(length(buffer), 2, "ByteBuffer expected to have 2 bytes written.");
573
+ }
574
+
575
+ #[test]
576
+ fn test_write_i32() {
577
+ let buffer = new();
578
+ write_i32(buffer, -65580);
579
+ assert_eq!(length(buffer), 4, "ByteBuffer expected to have 4 bytes written.");
580
+ }
581
+
582
+ #[test]
583
+ fn test_write_i64() {
584
+ let buffer = new();
585
+ write_i64(buffer, -2147888888);
586
+ assert_eq!(length(buffer), 8, "ByteBuffer expected to have 8 bytes written.");
587
+ }
588
+
589
+ #[test]
590
+ fn test_write_bit() {
591
+ let buffer = new();
592
+ write_bit(buffer, true);
593
+ write_bit(buffer, false);
594
+ write_bit(buffer, true);
595
+ assert_eq!(length(buffer), 1);
596
+ write_bit(buffer, true);
597
+ assert_eq!(length(buffer), 1);
598
+ write_bit(buffer, false);
599
+ assert_eq!(length(buffer), 1);
600
+ write_bit(buffer, true);
601
+ assert_eq!(length(buffer), 1);
602
+ write_bit(buffer, false);
603
+ assert_eq!(length(buffer), 1);
604
+ write_bit(buffer, true);
605
+ assert_eq!(length(buffer), 1);
606
+ write_bit(buffer, false);
607
+ assert_eq!(length(buffer), 2);
608
+ }
609
+
610
+ #[test]
611
+ fn test_write_bits() {
612
+ let buffer = new();
613
+ write_bits(buffer, 96, 8);
614
+ assert_eq!(length(buffer), 1);
615
+ }
616
+
617
+ #[test]
618
+ fn test_write_f32() {
619
+ let buffer = new();
620
+ write_f32(buffer, -21478888.9848);
621
+ assert_eq!(length(buffer), 4, "ByteBuffer expected to have 8 bytes written.");
622
+ }
623
+
624
+ #[test]
625
+ fn test_write_f64() {
626
+ let buffer = new();
627
+ write_f64(buffer, -2147888888.3282);
628
+ assert_eq!(length(buffer), 8, "ByteBuffer expected to have 8 bytes written.");
629
+ }
630
+
631
+ #[test]
632
+ fn test_read_u8() {
633
+ let buffer = new();
634
+ write_u8(buffer, 127);
635
+ write_u8(buffer, 100);
636
+ write_u8(buffer, 32);
637
+ assert_eq!(read_u8(buffer), 127, "ByteBuffer expected to have read 127.");
638
+ assert_eq!(read_u8(buffer), 100, "ByteBuffer expected to have read 100.");
639
+ assert_eq!(read_u8(buffer), 32, "ByteBuffer expected to have read 32.");
640
+ }
641
+
642
+ #[test]
643
+ fn test_read_i8() {
644
+ let buffer = new();
645
+ write_i8(buffer, -127);
646
+ write_i8(buffer, -100);
647
+ write_i8(buffer, -32);
648
+ assert_eq!(read_i8(buffer), -127, "ByteBuffer expected to have read -127.");
649
+ assert_eq!(read_i8(buffer), -100, "ByteBuffer expected to have read -100.");
650
+ assert_eq!(read_i8(buffer), -32, "ByteBuffer expected to have read -32.");
651
+ }
652
+
653
+ #[test]
654
+ fn test_read_u16() {
655
+ let buffer = new();
656
+ write_u16(buffer, 21411);
657
+ write_u16(buffer, 21499);
658
+ write_u16(buffer, 21488);
659
+ assert_eq!(read_u16(buffer), 21411, "ByteBuffer expected to have read 21411.");
660
+ assert_eq!(read_u16(buffer), 21499, "ByteBuffer expected to have read 21499.");
661
+ assert_eq!(read_u16(buffer), 21488, "ByteBuffer expected to have read 21488.");
662
+ }
663
+
664
+ #[test]
665
+ fn test_read_i16() {
666
+ let buffer = new();
667
+ write_i16(buffer, -21411);
668
+ write_i16(buffer, -21499);
669
+ write_i16(buffer, -21488);
670
+ assert_eq!(read_i16(buffer), -21411, "ByteBuffer expected to have read -21411.");
671
+ assert_eq!(read_i16(buffer), -21499, "ByteBuffer expected to have read -21499.");
672
+ assert_eq!(read_i16(buffer), -21488, "ByteBuffer expected to have read -21488.");
673
+ }
674
+
675
+ #[test]
676
+ fn test_read_u32() {
677
+ let buffer = new();
678
+ write_u32(buffer, 214000000);
679
+ write_u32(buffer, 214000099);
680
+ write_u32(buffer, 214000098);
681
+ assert_eq!(read_u32(buffer), 214000000, "ByteBuffer expected to have read 214000000.");
682
+ assert_eq!(read_u32(buffer), 214000099, "ByteBuffer expected to have read 214000099.");
683
+ assert_eq!(read_u32(buffer), 214000098, "ByteBuffer expected to have read 214000098.");
684
+ }
685
+
686
+ #[test]
687
+ fn test_read_i32() {
688
+ let buffer = new();
689
+ write_i32(buffer, -214000000);
690
+ write_i32(buffer, -214000099);
691
+ write_i32(buffer, -214000098);
692
+ assert_eq!(read_i32(buffer), -214000000, "ByteBuffer expected to have read -214000000.");
693
+ assert_eq!(read_i32(buffer), -214000099, "ByteBuffer expected to have read -214000099.");
694
+ assert_eq!(read_i32(buffer), -214000098, "ByteBuffer expected to have read -214000098.");
695
+ }
696
+
697
+ #[test]
698
+ fn test_read_u64() {
699
+ let buffer = new();
700
+ write_u64(buffer, 2149000000);
701
+ write_u64(buffer, 2149000099);
702
+ write_u64(buffer, 2149000098);
703
+ assert_eq!(read_i64(buffer), 2149000000, "ByteBuffer expected to have read 2149000000.");
704
+ assert_eq!(read_i64(buffer), 2149000099, "ByteBuffer expected to have read 2149000099.");
705
+ assert_eq!(read_i64(buffer), 2149000098, "ByteBuffer expected to have read 2149000098.");
706
+ }
707
+
708
+ #[test]
709
+ fn test_read_i64() {
710
+ let buffer = new();
711
+ write_i64(buffer, -2149000000);
712
+ write_i64(buffer, -2149000099);
713
+ write_i64(buffer, -2149000098);
714
+ assert_eq!(read_i64(buffer), -2149000000, "ByteBuffer expected to have read -2149000000.");
715
+ assert_eq!(read_i64(buffer), -2149000099, "ByteBuffer expected to have read -2149000099.");
716
+ assert_eq!(read_i64(buffer), -2149000098, "ByteBuffer expected to have read -2149000098.");
717
+ }
718
+
719
+ #[test]
720
+ fn test_read_bit() {
721
+ let buffer = new();
722
+ write_bit(buffer, true);
723
+ write_bit(buffer, false);
724
+ write_bit(buffer, true);
725
+ write_bit(buffer, true);
726
+
727
+ assert!(read_bit(buffer));
728
+ assert!(!read_bit(buffer));
729
+ assert!(read_bit(buffer));
730
+ assert!(read_bit(buffer));
731
+ }
732
+
733
+ #[test]
734
+ fn test_read_bits() {
735
+ let buffer = new();
736
+ write_bits(buffer, 96, 8);
737
+ assert_eq!(read_bits(buffer, 8), 96u64);
738
+ }
739
+ }