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
// Copyright (c) 2015-2016 lcov-parser developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::io;
use std::cmp::PartialEq;
use std::fmt:: { Display, Formatter, Result };
use std::collections::btree_map:: { BTreeMap };
use std::convert::{ From };
use record:: { BranchData };
use merger::ops:: { TryMerge, MergeResult, MergeBranch, BranchError };
use record:: { RecordWrite };
use report::summary:: { Summary };
use report::attribute:: { LineNumber, ExecutionCount };
use report::counter:: { Hit, HitFoundCounter, FoundCounter, HitCounter };

/// Units of the branch
///
/// # Examples
///
/// ```
/// use lcov_parser::branch::BranchUnit;
///
/// let branch1 = BranchUnit::new(1, 1);
/// let branch2 = BranchUnit::new(1, 1);
///
/// assert!(branch1 == branch2);
///
/// let not_eq_branch1 = BranchUnit::new(1, 1);
/// let not_eq_branch2 = BranchUnit::new(1, 2);
///
/// assert!(not_eq_branch1 != not_eq_branch2);
/// ```
#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone)]
pub struct BranchUnit(u32, u32);

impl BranchUnit {
    pub fn new(block: u32, branch: u32) -> BranchUnit {
        BranchUnit(block, branch)
    }
    pub fn block(&self) -> &u32 {
        &self.0
    }
    pub fn branch(&self) -> &u32 {
        &self.1
    }
}

impl Display for BranchUnit {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "{}-{}", self.0, self.1)
    }
}

#[derive(Debug, Clone)]
pub struct Branch {
    line_number: LineNumber,
    block: u32,
    branch: u32,
    execution_count: ExecutionCount
}

impl Branch {
    pub fn new(
        line_number: LineNumber,
        block: u32,
        branch: u32,
        execution_count: ExecutionCount
    ) -> Self {
        Branch {
            line_number: line_number,
            block: block,
            branch: branch,
            execution_count: execution_count
        }
    }
    pub fn line_number(&self) -> &LineNumber {
        &self.line_number
    }
    pub fn block(&self) -> &u32 {
        &self.block
    }
    pub fn branch(&self) -> &u32 {
        &self.branch
    }
    pub fn execution_count(&self) -> &ExecutionCount {
        &self.execution_count
    }
}

impl PartialEq<BranchData> for Branch {
    fn eq(&self, data: &BranchData) -> bool {
        if self.line_number != data.line {
            return false;
        }
        let branch_matched = self.block == data.block && self.branch == data.branch;

        if !branch_matched {
            return false;
        }
        return true;
    }
}

impl PartialEq<Branch> for Branch {
    fn eq(&self, other: &Branch) -> bool {
        if self.line_number != *other.line_number() {
            return false;
        }
        let branch_matched = self.block == *other.block() && self.branch == *other.branch();

        if !branch_matched {
            return false;
        }
        return true;
    }
}







impl<'a> From<&'a BranchData> for Branch {
    fn from(data: &'a BranchData) -> Self {
        Branch::new(
            data.line,
            data.block,
            data.branch,
            data.taken
        )
    }
}

impl<'a> TryMerge<&'a BranchData> for Branch {
    type Err = BranchError;

    fn try_merge(&mut self, data: &'a BranchData) -> MergeResult<Self::Err> {
        if self != data {
            return Err(
                BranchError::Mismatch(
                    MergeBranch::from(&self.clone()),
                    MergeBranch::from(data)
                )
            );
        }
        self.execution_count += data.taken;
        Ok(())
    }
}

impl<'a> TryMerge<&'a Branch> for Branch {
    type Err = BranchError;

    fn try_merge(&mut self, other: &'a Branch) -> MergeResult<Self::Err> {
        if self != other {
            return Err(
                BranchError::Mismatch(
                    MergeBranch::from(&self.clone()),
                    MergeBranch::from(other)
                )
            );
        }
        self.execution_count += *other.execution_count();
        Ok(())
    }
}

impl Hit for Branch {
    fn is_hit(&self) -> bool {
        self.execution_count.is_hit()
    }
}




#[derive(Debug, PartialEq, Clone)]
pub struct BranchBlocks {
    blocks: BTreeMap<BranchUnit, Branch>
}

impl BranchBlocks {
    pub fn new() -> Self {
        BranchBlocks {
            blocks: BTreeMap::new()
        }
    }
}

impl_summary!(BranchBlocks, blocks<BranchUnit, Branch>);


impl HitCounter for BranchBlocks {
    fn hit_count(&self) -> usize {
        self.iter()
            .filter(|&(_, branch)| branch.is_hit())
            .count()
    }
}

impl FoundCounter for BranchBlocks {
    fn found_count(&self) -> usize {
        self.blocks.len()
    }
}

impl HitFoundCounter for BranchBlocks {}



impl<'a> TryMerge<&'a BranchData> for BranchBlocks {
    type Err = BranchError;

    fn try_merge(&mut self, data: &'a BranchData) -> MergeResult<Self::Err> {
        let unit = BranchUnit::new(data.block, data.branch);
        if !self.blocks.contains_key(&unit) {
            self.blocks.insert(unit, Branch::from(data));
            return Ok(());
        }
        let block = self.blocks.get_mut(&unit).unwrap();
        block.try_merge(data)
    }
}

impl_try_merge_self_summary!(BranchBlocks:blocks, BranchError);



#[derive(Debug, Clone)]
pub struct Branches {
    branches: BTreeMap<LineNumber, BranchBlocks>
}

impl Branches {
    pub fn new() -> Self {
        Branches {
            branches: BTreeMap::new()
        }
    }
}

impl HitCounter for Branches {
    fn hit_count(&self) -> usize {
        self.iter()
            .map(|(_, blocks)| blocks.hit_count() )
            .sum()
    }
}

impl FoundCounter for Branches {
    fn found_count(&self) -> usize {
        self.iter()
            .map(|(_, blocks)| blocks.found_count() )
            .sum()
    }
}

impl HitFoundCounter for Branches {}


impl_summary!(Branches, branches<LineNumber, BranchBlocks>);


impl RecordWrite for Branches {
    fn write_records<T: io::Write>(&self, output: &mut T) -> io::Result<()> {
        write!(output, "{}", self)
    }
}

impl Display for Branches {
    fn fmt(&self, f: &mut Formatter) -> Result {
        if self.is_empty() {
            return Ok(());
        }
        for (line_number, blocks) in self.iter() {
            for (_, branch) in blocks.iter() {
                writeln!(f, "BRDA:{},{},{},{}",
                    line_number, branch.block(), branch.branch(), branch.execution_count())?;
            }
        }
        writeln!(f, "BRF:{}", self.found_count())?;
        writeln!(f, "BRH:{}", self.hit_count())?;
        Ok(())
    }
}

impl_try_merge_self_summary!(Branches:branches, BranchError);


impl<'a> TryMerge<&'a BranchData> for Branches {
    type Err = BranchError;

    fn try_merge(&mut self, data: &'a BranchData) -> MergeResult<Self::Err> {
        if self.branches.contains_key(&data.line) {
            let blocks = self.branches.get_mut(&data.line).unwrap();
            blocks.try_merge(data)
        } else {
            let blocks = {
                let mut blocks = BranchBlocks::new();
                let _ = blocks.try_merge(data)?;
                blocks
            };
            self.branches.insert(
                data.line.clone(),
                blocks
            );
            Ok(())
        }
    }
}


#[cfg(test)]
mod tests {
    use std::collections:: { HashMap };
    use merger::ops::*;
    use record:: { BranchData };
    use report::branch:: { Branch, BranchUnit, Branches, BranchBlocks };
    use report::summary:: { Summary };
    use report::counter:: { FoundCounter, HitCounter };

    #[test]
    fn branch_unit() {
        let branch1 = BranchUnit(1, 1);
        let branch2 = BranchUnit(1, 2);

        assert!(branch1 != branch2);

        let same_branch1 = BranchUnit(1, 1);
        let same_branch2 = BranchUnit(1, 1);
    
        assert_eq!(same_branch1, same_branch2);
    }

    #[test]
    fn branch_unit_as_hash_key() {
        let mut container = HashMap::new();
        container.insert(BranchUnit(1, 1), 1);

        assert!( container.contains_key(&BranchUnit(1, 1)) );
    }

    #[test]
    fn add_branch_data() {
        let mut branches = BranchBlocks::new();
        let b1 = &BranchData { line: 1, block: 0, branch: 1, taken: 1 };
        let b2 = &BranchData { line: 1, block: 0, branch: 1, taken: 1 };

        branches.try_merge(b1).unwrap();
        branches.try_merge(b2).unwrap();

        let branch = Branch::new(1, 0, 1, 2);
        assert_eq!(branches.get(&BranchUnit::new(0, 1)), Some(&branch));
    }

    #[test]
    fn append_branches() {
        let mut branches = BranchBlocks::new();
        let b1 = &BranchData { line: 1, block: 0, branch: 1, taken: 1 };
        let b2 = &BranchData { line: 1, block: 0, branch: 1, taken: 1 };

        branches.try_merge(b1).unwrap();
        branches.try_merge(b2).unwrap();

        let cloned_branches = branches.clone();
        branches.try_merge(&cloned_branches).unwrap();

        let branch = Branch::new(1, 0, 1, 2);
        assert_eq!(branches.get(&BranchUnit::new(0, 1)), Some(&branch));
    }

    #[test]
    fn branch_blocks_hit_count_and_found_count() {
        let mut branches = BranchBlocks::new();
        let b1 = &BranchData { line: 1, block: 0, branch: 1, taken: 1 };
        let b2 = &BranchData { line: 1, block: 0, branch: 2, taken: 0 };

        branches.try_merge(b1).unwrap();
        branches.try_merge(b2).unwrap();

        assert_eq!(branches.hit_count(), 1);
        assert_eq!(branches.found_count(), 2);
    }

    #[test]
    fn branches_hit_count_and_found_count() {
        let mut branches = Branches::new();
        branches.try_merge(&BranchData { line: 1, block: 0, branch: 1, taken: 1 }).unwrap();
        branches.try_merge(&BranchData { line: 1, block: 0, branch: 2, taken: 0 }).unwrap();

        assert_eq!(branches.hit_count(), 1);
        assert_eq!(branches.found_count(), 2);
    }
}