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

mattwparas / steel / 11924200389

20 Nov 2024 12:20AM UTC coverage: 46.725% (-0.2%) from 46.956%
11924200389

Pull #290

github

web-flow
Merge faebbd075 into a6f7286de
Pull Request #290: Fix provides within macros

37 of 263 new or added lines in 7 files covered. (14.07%)

27 existing lines in 11 files now uncovered.

12469 of 26686 relevant lines covered (46.72%)

483326.6 hits per line

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

81.25
/crates/steel-core/src/steel_vm/lazy_stream.rs
1
use super::vm::VmCore;
2
use crate::parser::span::Span;
3
use crate::rvals::{Result, SteelVal};
4
use std::cell::RefCell;
5
use std::rc::Rc;
6

7
use crate::values::lazy_stream::LazyStream;
8

9
// Used for inlining stream iterators
10
pub(crate) struct LazyStreamIter<'global, 'a> {
11
    stream: LazyStream,
12
    vm_ctx: Rc<RefCell<&'global mut VmCore<'a>>>,
13
    cur_inst_span: &'global Span,
14
}
15

16
impl<'global, 'a> LazyStreamIter<'global, 'a> {
UNCOV
17
    pub fn new(
×
18
        stream: LazyStream,
19
        vm_ctx: Rc<RefCell<&'global mut VmCore<'a>>>,
20
        cur_inst_span: &'global Span,
21
    ) -> Self {
22
        Self {
23
            stream,
24
            vm_ctx,
25
            cur_inst_span,
26
        }
27
    }
28
}
29

30
impl<'global, 'a> Iterator for LazyStreamIter<'global, 'a> {
31
    type Item = Result<SteelVal>;
32
    fn next(&mut self) -> Option<Self::Item> {
35✔
33
        if self.stream.empty_stream {
35✔
34
            return None;
×
35
        }
36

37
        let stream_first = self.stream.stream_first();
35✔
38
        let stream_thunk = self.stream.stream_thunk();
35✔
39

40
        let next_value = self.vm_ctx.borrow_mut().call_func_or_else_many_args(
35✔
41
            &stream_thunk,
35✔
42
            Vec::new(),
35✔
43
            self.cur_inst_span,
35✔
44
            throw!(TypeMismatch => format!("stream expected a thunk, found: {stream_thunk}")),
35✔
45
        );
46

47
        if let Ok(next_value) = next_value {
35✔
48
            if let SteelVal::StreamV(lazy_stream) = next_value {
70✔
49
                self.stream = lazy_stream.unwrap();
35✔
50
            } else {
51
                panic!("Lazy stream not implemented for the given type");
×
52
            }
53
        }
54

55
        Some(Ok(stream_first))
35✔
56
    }
57
}
58

59
#[cfg(test)]
60
mod stream_tests {
61
    use crate::steel_vm::test_util::assert_script;
62

63
    #[test]
64
    fn simple_stream() {
65
        let script = r#"
66
        (define (stream-cdr stream)
67
            ((#%stream-cdr stream)))
68

69
        (define (integers n)
70
            (stream-cons n (lambda () (integers (+ 1 n)))))
71

72
        (define (stream-section n stream)
73
            (cond ((= n 0) '())
74
                  (else
75
                   (cons
76
                    (stream-car stream)
77
                    (stream-section
78
                     (- n 1)
79
                     (stream-cdr stream))))))
80

81
        (assert! (equal? (list 0 1 2 3 4) (stream-section 5 (integers 0))))
82
        "#;
83
        assert_script(script);
84
    }
85

86
    #[test]
87
    fn simple_stream_with_map() {
88
        let script = r#"
89
        (define (stream-cdr stream)
90
            ((#%stream-cdr stream)))
91

92
        (define (integers n)
93
            (stream-cons n (lambda () (integers (+ 1 n)))))
94

95
        (define (stream-section n stream)
96
            (cond ((= n 0) '())
97
                  (else
98
                   (cons
99
                    (stream-car stream)
100
                    (stream-section
101
                     (- n 1)
102
                     (stream-cdr stream))))))
103

104
        (define (map-stream func s)
105
            (cond
106
                [(stream-empty? s) s]
107
                [else
108
                    (stream-cons (func (stream-car s))
109
                                    (lambda ()
110
                                    (map-stream func (stream-cdr s))))]))
111

112
        (assert! 
113
            (equal? (list 10 10 10 10 10)
114
                    (stream-section 5 (map-stream (lambda (x) 10) (integers 0)))))
115
        "#;
116
        assert_script(script);
117
    }
118

119
    #[test]
120
    fn simple_stream_with_transducer() {
121
        let script = r#"
122
        (define (stream-cdr stream)
123
            ((#%stream-cdr stream)))
124

125
        (define (integers n)
126
            (stream-cons n (lambda () (integers (+ 1 n)))))
127

128
        (assert! 
129
            (equal? (list 0 1 2 3 4)
130
                    (transduce (integers 0) (taking 5) (into-list))))
131
        "#;
132
        assert_script(script);
133
    }
134
}
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