• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ergoplatform / sigma-rust / 9822292740

06 Jul 2024 09:42PM UTC coverage: 78.938% (-1.6%) from 80.517%
9822292740

push

github

web-flow
Merge pull request #752 from SethDusek/optimizations

Optimizations

478 of 574 new or added lines in 119 files covered. (83.28%)

252 existing lines in 65 files now uncovered.

10794 of 13674 relevant lines covered (78.94%)

3.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/ergotree-interpreter/src/eval/byte_array_to_long.rs
1
use ergotree_ir::mir::byte_array_to_long::ByteArrayToLong;
2
use ergotree_ir::mir::value::Value;
3

4
use crate::eval::env::Env;
5
use crate::eval::Context;
6
use crate::eval::EvalError;
7
use crate::eval::EvalError::UnexpectedValue;
8
use crate::eval::Evaluable;
9
use ergotree_ir::mir::constant::TryExtractInto;
10

11
impl Evaluable for ByteArrayToLong {
12
    fn eval<'ctx>(
1✔
13
        &self,
14
        env: &mut Env<'ctx>,
15
        ctx: &Context<'ctx>,
16
    ) -> Result<Value<'ctx>, EvalError> {
17
        let input = self.input.eval(env, ctx)?.try_extract_into::<Vec<u8>>()?;
1✔
18
        if input.len() < 8 {
2✔
19
            return Err(UnexpectedValue(
1✔
20
                "byteArrayToLong: array must contain at least 8 elements".into(),
1✔
21
            ));
22
        }
23
        Ok(((input[0] as i64) << 56
9✔
24
            | (input[1] as i64) << 48
1✔
25
            | (input[2] as i64) << 40
1✔
26
            | (input[3] as i64) << 32
1✔
27
            | (input[4] as i64) << 24
1✔
28
            | (input[5] as i64) << 16
1✔
29
            | (input[6] as i64) << 8
1✔
30
            | (input[7] as i64))
1✔
UNCOV
31
            .into())
×
32
    }
33
}
34

35
#[cfg(test)]
36
#[allow(clippy::unwrap_used)]
37
mod tests {
38

39
    use super::*;
40
    use crate::eval::tests::try_eval_out_wo_ctx;
41

42
    fn eval_node(val: Vec<i8>) -> Result<i64, EvalError> {
43
        let expr = ByteArrayToLong {
44
            input: Box::new(val.into()),
45
        }
46
        .into();
47
        try_eval_out_wo_ctx(&expr)
48
    }
49

50
    #[test]
51
    fn eval_1() {
52
        let res = eval_node(vec![0, 0, 0, 0, 0, 0, 0, 1]);
53
        assert_eq!(res, Ok(1));
54
    }
55

56
    // Bytes after first 8 are ignored
57
    #[test]
58
    fn eval_skip_tail() {
59
        let res = eval_node(vec![0, 0, 0, 0, 0, 0, 0, 1, 0x42, 0x42]);
60
        assert_eq!(res, Ok(1));
61
    }
62

63
    #[test]
64
    fn eval_neg1() {
65
        let res = eval_node(vec![-1; 8]);
66
        assert_eq!(res, Ok(-1));
67
    }
68

69
    #[test]
70
    fn fails_for_short() {
71
        let res = eval_node(vec![0; 7]);
72
        assert!(res.is_err());
73
    }
74
    // Test equivalence between scala sigmastate-interpreter byteArrayToLong and rust impl
75
    // https://github.com/ScorexFoundation/sigmastate-interpreter/blob/efa68e7079393d70d25d43dd63dfba9a18d03415/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala#L3914
76
    #[test]
77
    fn test_equivalence() {
78
        let res = eval_node(
79
            base16::decode("712d7f00ff807f7f")
80
                .unwrap()
81
                .into_iter()
82
                .map(|b| b as i8)
83
                .collect(),
84
        );
85
        assert_eq!(res, Ok(8155314142501175167));
86
        let res = eval_node(
87
            base16::decode("812d7f00ff807f7f0101018050757f0580ac009680f2ffc1")
88
                .unwrap()
89
                .into_iter()
90
                .map(|b| b as i8)
91
                .collect(),
92
        );
93
        assert_eq!(res, Ok(-9138508426601529473));
94
    }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc