c_math 0.1.1

这是一个用于基础数据转换操作的一个包
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use num_traits::{Unsigned, Zero};
use std::any::{Any, TypeId};
use std::fmt::{format, Debug};
use std::io::{Error, ErrorKind};
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Not, Rem, Shl, Shr, Sub};

///从整数中读取某一位,这是一个泛型方法,性能有损失
pub fn get_bit_from_u<T>(data: T, index: u8, one: T, zero: T) -> Result<bool, Error>
where
    T: Unsigned
        + Copy
        + Clone
        + Debug
        + Default
        + PartialEq
        + PartialOrd
        + Add<Output = T>
        + Sub<Output = T>
        + Mul<Output = T>
        + Div<Output = T>
        + Rem<Output = T>
        + BitAnd<Output = T>
        + BitOr<Output = T>
        + BitXor<Output = T>
        + Not<Output = T>
        + Shl<u8, Output = T>
        + Shr<u8, Output = T>
        + 'static,
{
    let mut max_index: u8 = 0;
    // let data_type=data.type_id();
    // let mut data_type: String = String::new();
    // const  u8_type:TypeId = TypeId::of::<u8>();
    // const  u16_type:TypeId = TypeId::of::<u16>();
    // const  u32_type:TypeId = TypeId::of::<u32>();
    // const  u64_type:TypeId = TypeId::of::<u64>();
    // const  u128_type:TypeId = TypeId::of::<u128>();


    //方法1:下面if else的写法是正确的
    // if TypeId::of::<u8>() == data_type {
    //     if index > 7 {
    //         return Err(Error::new(
    //             ErrorKind::InvalidData,
    //             format!("index 值超出了U8位的索引范围"),
    //         ));
    //     }
    //     max_index = 7;
    //     // data_type = "u8".to_string();
    // } else if TypeId::of::<u16>() == data_type {
    //     if index > 15 {
    //         return Err(Error::new(
    //             ErrorKind::InvalidData,
    //             format!("index 值超出了U16位的索引范围"),
    //         ));
    //     }
    //     max_index = 15;
    //     // data_type = "u16".to_string();
    // } else if TypeId::of::<u32>() == data_type {
    //     if index > 31 {
    //         return Err(Error::new(
    //             ErrorKind::InvalidData,
    //             format!("index 值超出了U32位的索引范围"),
    //         ));
    //     }
    //     max_index = 31;
    //     // data_type = "u32".to_string();
    // } else if TypeId::of::<u64>() == data_type {
    //     if index > 63 {
    //         return Err(Error::new(
    //             ErrorKind::InvalidData,
    //             format!("index 值超出了U64位的索引范围"),
    //         ));
    //     }
    //     max_index = 63;
    //     // data_type = "u64".to_string();
    // } else if TypeId::of::<u128>() == data_type {
    //     if index > 127 {
    //         return Err(Error::new(
    //             ErrorKind::InvalidData,
    //             format!("index 值超出了U128位的索引范围"),
    //         ));
    //     }
    //     max_index = 127;
    //     // data_type = "u128".to_string();
    // } else {
    //     return Err(Error::new(
    //         ErrorKind::InvalidData,
    //         format!("输入的数据类型不是无符号整数"),
    //     ));
    // }
    //方法2:用macth语句绑定
    // let u8_type = TypeId::of::<u8>();
    // let u16_type = TypeId::of::<u16>();
    // let u32_type = TypeId::of::<u32>();
    // let u64_type = TypeId::of::<u64>();
    // let u128_type = TypeId::of::<u128>();

    match data.type_id() {
        u8_type if u8_type==TypeId::of::<u8>() => {
            if index > 7 {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("index 值超出了U8位的索引范围"),
                ));
            }
            max_index = 7;
            // data_type = "u8".to_string();

        },
        u16_type if u16_type==TypeId::of::<u16>() => {
            if index > 15 {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("index 值超出了U16位的索引范围"),
                ));
            }
            max_index = 15;
            // data_type = "u16".to_string();

        },
        u32_type if u32_type==TypeId::of::<u32>() => {
            if index > 31 {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("index 值超出了U32位的索引范围"),
                ));
            }
            max_index = 31;
            // data_type = "u32".to_string();

        },
        u64_type if u64_type==TypeId::of::<u64>() => {
            if index > 63 {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("index 值超出了U64位的索引范围"),
                ));
            }
            max_index = 63;
            // data_type = "u64".to_string();

        },
        u128_type if u128_type==TypeId::of::<u128>() => {
            if index > 127 {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("index 值超出了U128位的索引范围"),
                ));
            }
            max_index = 127;
            // data_type = "u128".to_string();

        },
        _ => {
            return Err(Error::new(
                ErrorKind::InvalidData,
                format!("输入的数据类型不是无符号整数"),
            ));
        },
    }

    Ok((data >> index) & one != zero)
}

///用于从u8中取出某一位,索引范围为0-7,
/// 如果索引超出7会报错,
/// 未加入大小端参数,由于大小端的不一样,造成数据不一样,可以更改相应的索引
pub fn get_bit_from_u8(data: u8, index: u8) -> Result<bool, Error> {
    if index > 7 {
        return Err(Error::new(
            ErrorKind::InvalidData,
            format!("index 值超出了U8位的索引范围"),
        ));
    }
    Ok((data >> index) & 1 != 0)
}

///用于从u16中取出某一位,索引范围为0-15,
///如果索引超出15会报错,
///未加入大小端参数,由于大小端的不一样,造成数据不一样,可以更改相应的索引
pub fn get_bit_from_u16(data: u16, index: u8) -> Result<bool, Error> {
    if index > 15 {
        return Err(Error::new(
            ErrorKind::InvalidData,
            format!("index 值超出了U16位的索引范围"),
        ));
    }
    Ok((data >> index) & 1 != 0)
}

///用于从u32中取出某一位,索引范围为0-31,
///如果索引超出31会报错,
///未加入大小端参数,由于大小端的不一样,造成数据不一样,可以更改相应的索引
pub fn get_bit_from_u32(data: u32, index: u8) -> Result<bool, Error> {
    if index > 31 {
        return Err(Error::new(
            ErrorKind::InvalidData,
            format!("index 值超出了U32位的索引范围"),
        ));
    }
    Ok((data >> index) & 1 != 0)
}

///用于从u64中取出某一位,索引范围为0-63,
///如果索引超出63会报错,
///未加入大小端参数,由于大小端的不一样,造成数据不一样,可以更改相应的索引
pub fn get_bit_from_u64(data: u64, index: u8) -> Result<bool, Error> {
    if index > 63 {
        return Err(Error::new(
            ErrorKind::InvalidData,
            format!("index 值超出了U64位的索引范围"),
        ));
    }
    Ok((data >> index) & 1 != 0)
}
//下面为获取多个位的操作
///用于从u8中取出多个位,最多位8位,
/// 如果超出8位会报错,
/// 未加入大小端参数,由于大小端的不一样,造成数据不一样,可以更改相应的索引
///  index中元素的数量代表要取的bool的个数,index中索引的值,代表要取的索引。
/// 示例:index结构vec![1,2,3],表示要取3位,三位的索引值分别为1,2,3
pub fn get_bits_from_u8(data: u8, index: Vec<u8>) -> Result<Vec<bool>, Error> {
    //1,对index进行校验
    if index.iter().count() > 8 {
        return Err(Error::new(
            ErrorKind::InvalidData,
            format!("index 成员数量超过了8个"),
        ));
    }
    for &item in index.iter() {
        if item > 7 {
            return Err(Error::new(
                ErrorKind::InvalidData,
                format!("index 成员的值超过了7"),
            ));
        }
    }
    //取出index中各个索引对应的位
    let mut value = Vec::<bool>::with_capacity(8);
    for i in 0..=7 {
        value.push((data >> i) & 1 != 0);
    }
    let mut vec_bool = Vec::<bool>::new();
    for &item in index.iter() {
        vec_bool.push(value[item as usize]);
    }

    Ok(vec_bool)
}

///用于从u16中取出多个位,最多位16位,
///如果超出16位会报错,
///未加入大小端参数,由于大小端的不一样,造成数据不一样,可以更改相应的索引,
///index中元素的数量代表要取的bool的个数,index中索引的值,代表要取的索引。
/// 示例:index结构vec![1,2,3],表示要取3位,三位的索引值分别为1,2,3
pub fn get_bits_from_u16(data: u16, index: Vec<u8>) -> Result<Vec<bool>, Error> {
    //1,对index进行校验
    if index.iter().count() > 16 {
        return Err(Error::new(
            ErrorKind::InvalidData,
            format!("index 成员数量超过了16个"),
        ));
    }
    for &item in index.iter() {
        if item > 15 {
            return Err(Error::new(
                ErrorKind::InvalidData,
                format!("index 成员的值超过了15"),
            ));
        }
    }
    //取出index中各个索引对应的位
    let mut value = Vec::<bool>::with_capacity(16);
    for i in 0..=15 {
        value.push((data >> i) & 1 != 0);
    }
    let mut vec_bool = Vec::<bool>::new();
    for &item in index.iter() {
        vec_bool.push(value[item as usize]);
    }

    Ok(vec_bool)
}

///用于从u32中取出多个位,最多位32位,
///如果超出32位会报错,
///未加入大小端参数,由于大小端的不一样,造成数据不一样,可以更改相应的索引,
///index中元素的数量代表要取的bool的个数,index中索引的值,代表要取的索引。
///示例:index结构vec![1,2,3],表示要取3位,三位的索引值分别为1,2,3
pub fn get_bits_from_u32(data: u32, index: Vec<u8>) -> Result<Vec<bool>, Error> {
    //1,对index进行校验
    if index.iter().count() > 32 {
        return Err(Error::new(
            ErrorKind::InvalidData,
            format!("index 成员数量超过了32个"),
        ));
    }
    for &item in index.iter() {
        if item > 31 {
            return Err(Error::new(
                ErrorKind::InvalidData,
                format!("index 成员的值超过了31"),
            ));
        }
    }
    //取出index中各个索引对应的位
    let mut value = Vec::<bool>::with_capacity(32);
    for i in 0..=31 {
        value.push((data >> i) & 1 != 0);
    }
    let mut vec_bool = Vec::<bool>::new();
    for &item in index.iter() {
        vec_bool.push(value[item as usize]);
    }

    Ok(vec_bool)
}

///用于从u64中取出多个位,最多位64位,
///如果超出64位会报错,
///未加入大小端参数,由于大小端的不一样,造成数据不一样,可以更改相应的索引,
///index中元素的数量代表要取的bool的个数,index中索引的值,代表要取的索引。
///示例:index结构vec![1,2,3],表示要取3位,三位的索引值分别为1,2,3
pub fn get_bits_from_u64(data: u64, index: Vec<u8>) -> Result<Vec<bool>, Error> {
    //1,对index进行校验
    if index.iter().count() > 64 {
        return Err(Error::new(
            ErrorKind::InvalidData,
            format!("index 成员数量超过了64个"),
        ));
    }
    for &item in index.iter() {
        if item > 63 {
            return Err(Error::new(
                ErrorKind::InvalidData,
                format!("index 成员的值超过了63"),
            ));
        }
    }
    //取出index中各个索引对应的位
    let mut value = Vec::<bool>::with_capacity(64);
    for i in 0..=63 {
        value.push((data >> i) & 1 != 0);
    }
    let mut vec_bool = Vec::<bool>::new();
    for &item in index.iter() {
        vec_bool.push(value[item as usize]);
    }

    Ok(vec_bool)
}
#[cfg(test)]
mod test {
    use super::*;
    use crate::get_bits::get_bit_from_u8;
    use std::fmt::format;
    use std::io::{Error, ErrorKind};

    #[test]
    fn test_get_bit_from_u8() {
        let data1: u8 = 0b1100_1100;
        assert_eq!(true, get_bit_from_u8(data1, 2).unwrap());
        assert_eq!(false, get_bit_from_u8(data1, 1).unwrap());
        assert_eq!(false, get_bit_from_u8(data1, 0).unwrap());
        assert_eq!(true, get_bit_from_u8(data1, 7).unwrap());
        //测试泛型函数
        assert_eq!(true, get_bit_from_u(data1, 2, 1, 0).unwrap());
        assert_eq!(false, get_bit_from_u(data1, 1, 1, 0).unwrap());
        assert_eq!(false, get_bit_from_u(data1, 0, 1, 0).unwrap());
        assert_eq!(true, get_bit_from_u(data1, 7, 1, 0).unwrap());
        // assert_eq!(Err(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围")))),get_bit_from_u8(data1,10).unwrap());
        // assert!(matches!(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围"))),get_bit_from_u8(data1,10)));
        // assert_eq!(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围"))),get_bit_from_u8(data1,10).unwrap_err());
        assert_eq!(
            Error::new(
                ErrorKind::InvalidData,
                format!("index 值超出了U8位的索引范围")
            )
            .to_string(),
            get_bit_from_u8(data1, 10).unwrap_err().to_string()
        );
        //测试泛型
        assert_eq!(
            Error::new(
                ErrorKind::InvalidData,
                format!("index 值超出了U8位的索引范围")
            )
            .to_string(),
            get_bit_from_u(data1, 10, 1, 0).unwrap_err().to_string()
        );
        println!("错误值{:?}", get_bit_from_u8(data1, 10));
    }

    #[test]
    fn test_get_bit_from_u16() {
        let data1: u16 = 0b1100_1100_1100_0011;
        assert_eq!(true, get_bit_from_u16(data1, 0).unwrap());
        assert_eq!(false, get_bit_from_u16(data1, 2).unwrap());
        assert_eq!(true, get_bit_from_u16(data1, 7).unwrap());
        assert_eq!(true, get_bit_from_u16(data1, 15).unwrap());
        //测试泛型
        assert_eq!(true, get_bit_from_u(data1, 0, 1, 0).unwrap());
        assert_eq!(false, get_bit_from_u(data1, 2, 1, 0).unwrap());
        assert_eq!(true, get_bit_from_u(data1, 7, 1, 0).unwrap());
        assert_eq!(true, get_bit_from_u(data1, 15, 1, 0).unwrap());
        // assert_eq!(Err(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围")))),get_bit_from_u8(data1,10).unwrap());
        // assert!(matches!(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围"))),get_bit_from_u8(data1,10)));
        // assert_eq!(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围"))),get_bit_from_u8(data1,10).unwrap_err());
        assert_eq!(
            Error::new(
                ErrorKind::InvalidData,
                format!("index 值超出了U16位的索引范围")
            )
            .to_string(),
            get_bit_from_u16(data1, 16).unwrap_err().to_string()
        );
        println!("错误值{:?}", get_bit_from_u16(data1, 16));
    }

    #[test]
    fn test_get_bit_from_u32() {
        let data1: u32 = 0b1100_1100_1100_0011_1100_1100_1100_0011;
        assert_eq!(true, get_bit_from_u32(data1, 0).unwrap());
        assert_eq!(false, get_bit_from_u32(data1, 2).unwrap());
        assert_eq!(true, get_bit_from_u32(data1, 7).unwrap());
        assert_eq!(true, get_bit_from_u32(data1, 31).unwrap());

        // assert_eq!(Err(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围")))),get_bit_from_u8(data1,10).unwrap());
        // assert!(matches!(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围"))),get_bit_from_u8(data1,10)));
        // assert_eq!(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围"))),get_bit_from_u8(data1,10).unwrap_err());
        assert_eq!(
            Error::new(
                ErrorKind::InvalidData,
                format!("index 值超出了U32位的索引范围")
            )
            .to_string(),
            get_bit_from_u32(data1, 32).unwrap_err().to_string()
        );
        //测试get_bit_from_u
        assert_eq!(true, get_bit_from_u(data1, 0,1,0).unwrap());
        assert_eq!(false, get_bit_from_u(data1, 2,1,0).unwrap());
        assert_eq!(true, get_bit_from_u(data1, 7,1,0).unwrap());
        assert_eq!(true, get_bit_from_u(data1, 31,1,0).unwrap());

        assert_eq!(
            Error::new(
                ErrorKind::InvalidData,
                format!("index 值超出了U32位的索引范围")
            )
                .to_string(),
            get_bit_from_u(data1, 32,1,0).unwrap_err().to_string()
        );
        println!("错误值{:?}", get_bit_from_u32(data1, 32));
    }
    #[test]
    fn test_get_bit_from_u64() {
        let data1: u64 =
            0b1100_1100_1100_0011_1100_1100_1100_0011_1100_1100_1100_0011_1100_1100_1100_0011;
        assert_eq!(true, get_bit_from_u64(data1, 0).unwrap());
        assert_eq!(false, get_bit_from_u64(data1, 2).unwrap());
        assert_eq!(true, get_bit_from_u64(data1, 7).unwrap());
        assert_eq!(true, get_bit_from_u64(data1, 31).unwrap());
        assert_eq!(true, get_bit_from_u64(data1, 32).unwrap());
        assert_eq!(false, get_bit_from_u64(data1, 34).unwrap());
        assert_eq!(true, get_bit_from_u64(data1, 39).unwrap());
        assert_eq!(true, get_bit_from_u64(data1, 63).unwrap());
        // assert_eq!(Err(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围")))),get_bit_from_u8(data1,10).unwrap());
        // assert!(matches!(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围"))),get_bit_from_u8(data1,10)));
        // assert_eq!(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围"))),get_bit_from_u8(data1,10).unwrap_err());
        assert_eq!(
            Error::new(
                ErrorKind::InvalidData,
                format!("index 值超出了U64位的索引范围")
            )
            .to_string(),
            get_bit_from_u64(data1, 64).unwrap_err().to_string()
        );
        //测试get_bit_from_u

        assert_eq!(true, get_bit_from_u(data1, 0,1,0).unwrap());
        assert_eq!(false, get_bit_from_u(data1, 2,1,0).unwrap());
        assert_eq!(true, get_bit_from_u(data1, 7,1,0).unwrap());
        assert_eq!(true, get_bit_from_u(data1, 31,1,0).unwrap());
        assert_eq!(true, get_bit_from_u(data1, 32,1,0).unwrap());
        assert_eq!(false, get_bit_from_u(data1, 34,1,0).unwrap());
        assert_eq!(true, get_bit_from_u(data1, 39,1,0).unwrap());
        assert_eq!(true, get_bit_from_u(data1, 63,1,0).unwrap());
        // assert_eq!(Err(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围")))),get_bit_from_u8(data1,10).unwrap());
        // assert!(matches!(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围"))),get_bit_from_u8(data1,10)));
        // assert_eq!(Box::new(Error::new(ErrorKind::InvalidData,format!("index 值超出了U8的索引范围"))),get_bit_from_u8(data1,10).unwrap_err());
        assert_eq!(
            Error::new(
                ErrorKind::InvalidData,
                format!("index 值超出了U64位的索引范围")
            )
                .to_string(),
            get_bit_from_u(data1, 64,1,0).unwrap_err().to_string()
        );
        println!("错误值{:?}", get_bit_from_u64(data1, 64));
    }

    #[test]
    fn test_get_bits_from_u8() {
        let data1: u8 = 0b1100_0011;
        let index: Vec<u8> = vec![1, 3, 5, 7];
        assert_eq!(
            vec![true, false, false, true],
            get_bits_from_u8(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![0, 2, 4, 6];
        assert_eq!(
            vec![true, false, false, true],
            get_bits_from_u8(data1, index).unwrap()
        );
        //测试index数量超过8个
        let index: Vec<u8> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
        assert_eq!(
            Error::new(ErrorKind::InvalidData, format!("index 成员数量超过了8个")).to_string(),
            get_bits_from_u8(data1, index).unwrap_err().to_string()
        );
        //测试成员中有索引超过7
        let index: Vec<u8> = vec![0, 1, 8];
        assert_eq!(
            Error::new(ErrorKind::InvalidData, format!("index 成员的值超过了7")).to_string(),
            get_bits_from_u8(data1, index).unwrap_err().to_string()
        );
    }

    #[test]
    fn test_get_bits_from_u16() {
        let data1: u16 = 0b1100_0011_1100_0011;
        let index: Vec<u8> = vec![1, 3, 5, 7, 9, 11, 13, 15];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u16(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![0, 2, 4, 6, 8, 10, 12, 14];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u16(data1, index).unwrap()
        );
        //测试index数量超过8个
        let index: Vec<u8> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
        assert_eq!(
            Error::new(ErrorKind::InvalidData, format!("index 成员数量超过了16个")).to_string(),
            get_bits_from_u16(data1, index).unwrap_err().to_string()
        );
        //测试成员中有索引超过7
        let index: Vec<u8> = vec![0, 1, 8, 16];
        assert_eq!(
            Error::new(ErrorKind::InvalidData, format!("index 成员的值超过了15")).to_string(),
            get_bits_from_u16(data1, index).unwrap_err().to_string()
        );
    }

    #[test]
    fn test_get_bits_from_u32() {
        let data1: u32 = 0b1100_0011_1100_0011_1100_0011_1100_0011;
        let index: Vec<u8> = vec![1, 3, 5, 7, 9, 11, 13, 15];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u32(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![17, 19, 21, 23, 25, 27, 29, 31];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u32(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![0, 2, 4, 6, 8, 10, 12, 14];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u32(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![16, 18, 20, 22, 24, 26, 28, 30];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u32(data1, index).unwrap()
        );
        //测试index数量超过32个
        let index: Vec<u8> = vec![
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
            24, 25, 26, 27, 28, 29, 30, 31, 32,
        ];
        assert_eq!(
            Error::new(ErrorKind::InvalidData, format!("index 成员数量超过了32个")).to_string(),
            get_bits_from_u32(data1, index).unwrap_err().to_string()
        );
        //测试成员中有索引超过7
        let index: Vec<u8> = vec![0, 1, 8, 32];
        assert_eq!(
            Error::new(ErrorKind::InvalidData, format!("index 成员的值超过了31")).to_string(),
            get_bits_from_u32(data1, index).unwrap_err().to_string()
        );
    }
    #[test]
    fn test_get_bits_from_u64() {
        let data1: u64 =
            0b1100_0011_1100_0011_1100_0011_1100_0011_1100_0011_1100_0011_1100_0011_1100_0011;
        let index: Vec<u8> = vec![1, 3, 5, 7, 9, 11, 13, 15];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u64(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![17, 19, 21, 23, 25, 27, 29, 31];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u64(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![0, 2, 4, 6, 8, 10, 12, 14];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u64(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![16, 18, 20, 22, 24, 26, 28, 30];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u64(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![33, 35, 37, 39, 41, 43, 45, 47];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u64(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![49, 51, 53, 55, 57, 59, 61, 63];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u64(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![32, 34, 36, 38, 40, 42, 44, 46];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u64(data1, index).unwrap()
        );
        let index: Vec<u8> = vec![48, 50, 52, 54, 56, 58, 60, 62];
        assert_eq!(
            vec![true, false, false, true, true, false, false, true],
            get_bits_from_u64(data1, index).unwrap()
        );
        //测试index数量超过64个
        let index: Vec<u8> = vec![
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
            24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
            46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
        ];
        assert_eq!(
            Error::new(ErrorKind::InvalidData, format!("index 成员数量超过了64个")).to_string(),
            get_bits_from_u64(data1, index).unwrap_err().to_string()
        );
        //测试成员中有索引超过7
        let index: Vec<u8> = vec![0, 1, 8, 64];
        assert_eq!(
            Error::new(ErrorKind::InvalidData, format!("index 成员的值超过了63")).to_string(),
            get_bits_from_u64(data1, index).unwrap_err().to_string()
        );
    }
}