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

ergoplatform / sigma-rust / 15667516698

15 Jun 2025 09:22PM UTC coverage: 77.672% (-0.6%) from 78.291%
15667516698

Pull #832

github

web-flow
Merge 39c40f47b into 6f12ef8f2
Pull Request #832: Soft Fork support

298 of 462 new or added lines in 72 files covered. (64.5%)

53 existing lines in 22 files now uncovered.

11765 of 15147 relevant lines covered (77.67%)

2.91 hits per line

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

68.97
/ergotree-interpreter/src/eval/coll_exists.rs
1
use alloc::string::ToString;
2
use alloc::vec::Vec;
3
use ergotree_ir::mir::coll_exists::Exists;
4
use ergotree_ir::mir::constant::TryExtractInto;
5
use ergotree_ir::mir::value::Value;
6

7
use crate::eval::env::Env;
8
use crate::eval::Context;
9
use crate::eval::EvalError;
10
use crate::eval::Evaluable;
11

12
impl Evaluable for Exists {
13
    fn eval<'ctx>(
1✔
14
        &self,
15
        env: &mut Env<'ctx>,
16
        ctx: &Context<'ctx>,
17
    ) -> Result<Value<'ctx>, EvalError> {
18
        let input_v = self.input.eval(env, ctx)?;
1✔
19
        let condition_v = self.condition.eval(env, ctx)?;
2✔
20
        let input_v_clone = input_v.clone();
1✔
21
        let mut condition_call = |arg: Value<'ctx>| match &condition_v {
3✔
22
            Value::Lambda(func_value) => {
1✔
23
                let func_arg = func_value.args.first().ok_or_else(|| {
2✔
24
                    EvalError::NotFound(
×
25
                        "Exists: evaluated condition has empty arguments list".to_string(),
×
26
                    )
27
                })?;
28
                let orig_val = env.get(func_arg.idx).cloned();
1✔
29
                env.insert(func_arg.idx, arg);
1✔
30
                let res = func_value.body.eval(env, ctx);
1✔
31
                if let Some(orig_val) = orig_val {
1✔
32
                    env.insert(func_arg.idx, orig_val);
×
33
                } else {
34
                    env.remove(&func_arg.idx);
2✔
35
                }
36
                res
1✔
37
            }
38
            _ => Err(EvalError::UnexpectedValue(format!(
×
39
                "expected Exists::condition to be Value::FuncValue got: {0:?}",
×
40
                input_v_clone
×
41
            ))),
42
        };
43
        let normalized_input_vals: Vec<Value> = match input_v {
3✔
44
            Value::Coll(coll) => Ok(coll.as_vec()),
1✔
UNCOV
45
            _ => Err(EvalError::UnexpectedValue(format!(
×
46
                "expected Map input to be Value::Coll, got: {0:?}",
×
47
                input_v
×
48
            ))),
49
        }?;
50

51
        for item in normalized_input_vals {
3✔
52
            let res = condition_call(item)?.try_extract_into::<bool>()?;
2✔
53
            if res {
1✔
54
                return Ok(true.into());
1✔
55
            }
56
        }
57
        Ok(false.into())
1✔
58
    }
59
}
60

61
#[allow(clippy::unwrap_used)]
62
#[cfg(test)]
63
mod tests {
64

65
    use crate::eval::test_util::eval_out_wo_ctx;
66

67
    use super::*;
68

69
    use alloc::boxed::Box;
70
    use ergotree_ir::mir::bin_op::BinOp;
71
    use ergotree_ir::mir::bin_op::RelationOp;
72
    use ergotree_ir::mir::expr::Expr;
73
    use ergotree_ir::mir::func_value::FuncArg;
74
    use ergotree_ir::mir::func_value::FuncValue;
75
    use ergotree_ir::mir::val_use::ValUse;
76
    use ergotree_ir::types::stype::SType;
77

78
    fn check(coll: Vec<i32>) {
79
        let body: Expr = BinOp {
80
            kind: RelationOp::Le.into(),
81
            left: Box::new(Expr::Const(1i32.into())),
82
            right: Box::new(
83
                ValUse {
84
                    val_id: 1.into(),
85
                    tpe: SType::SBox,
86
                }
87
                .into(),
88
            ),
89
        }
90
        .into();
91
        let expr: Expr = Exists::new(
92
            coll.clone().into(),
93
            FuncValue::new(
94
                vec![FuncArg {
95
                    idx: 1.into(),
96
                    tpe: SType::SInt,
97
                }],
98
                body,
99
            )
100
            .into(),
101
        )
102
        .unwrap()
103
        .into();
104
        assert_eq!(
105
            eval_out_wo_ctx::<bool>(&expr),
106
            coll.iter().any(|it| 1 <= *it)
107
        );
108
    }
109

110
    #[test]
111
    fn eval_emty_coll() {
112
        check(Vec::<i32>::new());
113
    }
114

115
    #[test]
116
    fn eval_true() {
117
        check(vec![1, 2]);
118
    }
119

120
    #[test]
121
    fn eval_false() {
122
        check(vec![2, 2]);
123
    }
124
}
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

© 2025 Coveralls, Inc