LCOV - code coverage report
Current view: top level - src/pattern - group.c (source / functions) Hit Total Coverage
Test: passgen-test.info Lines: 27 27 100.0 %
Date: 2024-11-29 06:05:05 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include "passgen/pattern/group.h"
       2             : #include "passgen/config.h"
       3             : #include "passgen/pattern/segment.h"
       4             : 
       5       33428 : void passgen_pattern_group_init(passgen_pattern_group *group) {
       6       33428 :     passgen_stack_init(&group->segments, sizeof(passgen_pattern_segment));
       7       33428 :     group->multiplier_sum = 0;
       8       33428 : }
       9             : 
      10       33428 : void passgen_pattern_group_free(passgen_pattern_group *group) {
      11       69971 :     for(size_t i = 0; i < group->segments.len; i++) {
      12             :         passgen_pattern_segment *segment =
      13       36543 :             passgen_stack_get(&group->segments, i);
      14             : 
      15       36543 :         passgen_pattern_segment_free(segment);
      16             :     }
      17             : 
      18       33428 :     passgen_stack_free(&group->segments);
      19             : 
      20       33428 :     PASSGEN_CLEAR(group);
      21       33428 : }
      22             : 
      23         350 : void passgen_pattern_group_finish(passgen_pattern_group *group) {
      24             :     // iterate through the segments and find out if there are any non-empty
      25             :     // ones.
      26         350 :     bool empty = true;
      27         745 :     for(size_t i = 0; i < group->segments.len; i++) {
      28             :         passgen_pattern_segment *segment =
      29         395 :             passgen_stack_get(&group->segments, i);
      30             : 
      31             :         // make this as non-empty only if it has items and a non-zero
      32             :         // multiplier. zero multiplier segments are removed during parsing,
      33             :         // so we can assume that all segments have a positive multiplier.
      34         395 :         if(segment->items.len > 0) {
      35         235 :             empty = false;
      36             :         }
      37             :     }
      38             : 
      39             :     // if no non-empty segments were found, set the multiplier sum to zero to
      40             :     // indicate that this group can be removed.
      41         350 :     if(empty) {
      42         154 :         group->multiplier_sum = 0;
      43             :     }
      44         350 : }
      45             : 
      46             : passgen_pattern_segment *
      47       36545 : passgen_pattern_group_segment_append(passgen_pattern_group *group) {
      48       36545 :     group->multiplier_sum += 1;
      49             :     passgen_pattern_segment *segment =
      50       36545 :         passgen_stack_push(&group->segments, NULL);
      51       36545 :     passgen_pattern_segment_init(segment);
      52             : 
      53       36545 :     return segment;
      54             : }
      55             : 
      56             : passgen_pattern_segment *
      57         130 : passgen_pattern_group_segment_get(passgen_pattern_group *group, size_t n) {
      58         130 :     return passgen_stack_get(&group->segments, n);
      59             : }

Generated by: LCOV version 1.14