LCOV - code coverage report
Current view: top level - src/tests - entropy.c (source / functions) Hit Total Coverage
Test: passgen-test.info Lines: 54 55 98.2 %
Date: 2024-11-29 06:05:05 Functions: 6 6 100.0 %

          Line data    Source code
       1             : #include "tests.h"
       2             : #include <math.h>
       3             : #include <passgen/generate.h>
       4             : #include <passgen/passgen.h>
       5             : 
       6          31 : double entropy(const char *p) {
       7             :     // parse pattern
       8             :     passgen_pattern pattern;
       9             :     passgen_error error;
      10          31 :     if(0 != passgen_parse(&pattern, &error, p)) {
      11           0 :         return NAN;
      12             :     }
      13             : 
      14          31 :     size_t buffer_len = 256;
      15          31 :     uint8_t buffer[buffer_len];
      16          31 :     buffer[0] = 0;
      17             :     passgen_random random;
      18          31 :     passgen_random_open(&random, NULL);
      19             :     passgen_env env;
      20          31 :     passgen_env_init(&env, &random);
      21             :     double entropy;
      22          31 :     int length = passgen_generate_fill_utf8(
      23             :         &pattern,
      24             :         &env,
      25             :         &entropy,
      26             :         &buffer[0],
      27             :         buffer_len);
      28          31 :     buffer[length] = 0;
      29          31 :     passgen_pattern_free(&pattern);
      30             : 
      31          31 :     return entropy;
      32             : }
      33             : 
      34          32 : bool equals(double a, double b) {
      35          32 :     double epsilon = 0.00001;
      36          32 :     return (a - epsilon) < b && (a + epsilon) > b;
      37             : }
      38             : 
      39           1 : test_result test_entropy_literal(void) {
      40           1 :     assert(equals(0.0, 0.0));
      41           1 :     assert(equals(1.0, entropy("")));
      42           1 :     assert(equals(1.0, entropy("a")));
      43           1 :     assert(equals(1.0, entropy("ab")));
      44           1 :     assert(equals(1.0, entropy("abc")));
      45           1 :     assert(equals(1.0, entropy("abcdefghijklmnopqrstuvwxyz")));
      46             : 
      47           1 :     return test_ok;
      48             : }
      49             : 
      50           1 : test_result test_entropy_group(void) {
      51           1 :     assert(equals(1.0, entropy("()")));
      52           1 :     assert(equals(1.0, entropy("(abc)")));
      53           1 :     assert(equals(1.0, entropy("(())")));
      54           1 :     assert(equals(2.0, entropy("(abc|def)")));
      55           1 :     assert(equals(3.0, entropy("(abc|def|hij)")));
      56           1 :     assert(equals(4.0, entropy("(abc|def|hij|klm)")));
      57             : 
      58           1 :     return test_ok;
      59             : }
      60             : 
      61           1 : test_result test_entropy_range(void) {
      62           1 :     assert(equals(1.0, entropy("[a]")));
      63           1 :     assert(equals(2.0, entropy("[ab]")));
      64           1 :     assert(equals(3.0, entropy("[abc]")));
      65           1 :     assert(equals(3.0, entropy("[a-c]")));
      66           1 :     assert(equals(26.0, entropy("[a-z]")));
      67           1 :     assert(equals(10.0, entropy("[0-9]")));
      68           1 :     assert(equals(16.0, entropy("[0-9a-f]")));
      69           1 :     assert(equals(20.0, entropy("[0-9a-f,.:\\-]")));
      70             : 
      71           1 :     return test_ok;
      72             : }
      73             : 
      74           1 : test_result test_entropy_repeat(void) {
      75           1 :     assert(equals(3.0, entropy("[abc]{1}")));
      76           1 :     assert(equals(3.0 * 3.0, entropy("[abc]{2}")));
      77           1 :     assert(equals(3.0 * 3.0 * 3.0, entropy("[abc]{3}")));
      78             : 
      79           1 :     assert(equals(3.0, entropy("(this|that|other){1}")));
      80           1 :     assert(equals(3.0 * 3.0, entropy("(this|that|other){2}")));
      81           1 :     assert(equals(3.0 * 3.0 * 3.0, entropy("(this|that|other){3}")));
      82             : 
      83           1 :     assert(equals(1.0, entropy("a{1}")));
      84           1 :     assert(equals(2.0, entropy("a{1,2}")));
      85           1 :     assert(equals(3.0, entropy("a{1,3}")));
      86             : 
      87           1 :     assert(equals(1.0, entropy("(a){1}")));
      88           1 :     assert(equals(2.0, entropy("(a){1,2}")));
      89           1 :     assert(equals(3.0, entropy("(a){1,3}")));
      90             : 
      91           1 :     return test_ok;
      92             : }

Generated by: LCOV version 1.14