LCOV - code coverage report
Current view: top level - libmogwai-tariff/tests - tariff-builder.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 76 76 100.0 %
Date: 2022-06-30 20:59:16 Functions: 5 5 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 20 38 52.6 %

           Branch data     Line data    Source code
       1                 :            : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
       2                 :            :  *
       3                 :            :  * Copyright © 2018 Endless Mobile, Inc.
       4                 :            :  *
       5                 :            :  * This library is free software; you can redistribute it and/or
       6                 :            :  * modify it under the terms of the GNU Lesser General Public
       7                 :            :  * License as published by the Free Software Foundation; either
       8                 :            :  * version 2.1 of the License, or (at your option) any later version.
       9                 :            :  *
      10                 :            :  * This library is distributed in the hope that it will be useful,
      11                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13                 :            :  * Lesser General Public License for more details.
      14                 :            :  *
      15                 :            :  * You should have received a copy of the GNU Lesser General Public
      16                 :            :  * License along with this library; if not, write to the Free Software
      17                 :            :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
      18                 :            :  *
      19                 :            :  * Authors:
      20                 :            :  *  - Philip Withnall <withnall@endlessm.com>
      21                 :            :  */
      22                 :            : 
      23                 :            : #include "config.h"
      24                 :            : 
      25                 :            : #include <glib.h>
      26                 :            : #include <gio/gio.h>
      27                 :            : #include <libmogwai-tariff/tariff-builder.h>
      28                 :            : #include <locale.h>
      29                 :            : 
      30                 :            : #include "common.h"
      31                 :            : 
      32                 :            : /* Test resetting an empty tariff builder doesn’t crash. */
      33                 :            : static void
      34                 :          1 : test_tariff_builder_reset_empty (void)
      35                 :            : {
      36                 :          1 :   g_autoptr(MwtTariffBuilder) builder = NULL;
      37                 :            : 
      38                 :          1 :   builder = mwt_tariff_builder_new ();
      39                 :            : 
      40                 :            :   /* Do it a couple of times for good measure. */
      41         [ +  + ]:          3 :   for (guint i = 0; i < 2; i++)
      42                 :            :     {
      43                 :          2 :       mwt_tariff_builder_reset (builder);
      44         [ -  + ]:          2 :       g_assert_null (mwt_tariff_builder_get_tariff (builder));
      45         [ -  + ]:          2 :       g_assert_null (mwt_tariff_builder_get_tariff_as_variant (builder));
      46         [ -  + ]:          2 :       g_assert_null (mwt_tariff_builder_get_tariff_as_bytes (builder));
      47                 :            :     }
      48                 :          1 : }
      49                 :            : 
      50                 :            : /* Test resetting a partially-filled tariff builder doesn’t crash. */
      51                 :            : static void
      52                 :          1 : test_tariff_builder_reset_partial (void)
      53                 :            : {
      54                 :          1 :   g_autoptr(MwtTariffBuilder) builder = NULL;
      55                 :            : 
      56                 :          1 :   builder = mwt_tariff_builder_new ();
      57                 :            : 
      58                 :          1 :   mwt_tariff_builder_set_name (builder, "test-tariff");
      59                 :            : 
      60                 :          2 :   g_autoptr(GDateTime) start1 = g_date_time_new_utc (2018, 1, 1, 0, 0, 0);
      61                 :          2 :   g_autoptr(GDateTime) end1 = g_date_time_new_utc (2018, 2, 1, 0, 0, 0);
      62                 :            : 
      63                 :          1 :   g_autoptr(MwtPeriod) period = NULL;
      64                 :          1 :   period = mwt_period_new (start1, end1, MWT_PERIOD_REPEAT_NONE, 0, NULL);
      65                 :          1 :   mwt_tariff_builder_add_period (builder, period);
      66                 :            : 
      67                 :          1 :   mwt_tariff_builder_reset (builder);
      68         [ -  + ]:          1 :   g_assert_null (mwt_tariff_builder_get_tariff (builder));
      69         [ -  + ]:          1 :   g_assert_null (mwt_tariff_builder_get_tariff_as_variant (builder));
      70         [ -  + ]:          1 :   g_assert_null (mwt_tariff_builder_get_tariff_as_bytes (builder));
      71                 :          1 : }
      72                 :            : 
      73                 :            : /* Test building a simple tariff. This tariff:
      74                 :            :  *  • Starts on 2018-01-01 and runs forever.
      75                 :            :  *  • Has period1 which limits monthly capacity to 2GB.
      76                 :            :  *  • Has period2 which uncaps the capacity each weekend.
      77                 :            :  */
      78                 :            : static void
      79                 :          1 : test_tariff_builder_simple (void)
      80                 :            : {
      81                 :          1 :   g_autoptr(MwtTariffBuilder) builder = NULL;
      82                 :            : 
      83                 :            :   /* Build the tariff. */
      84                 :          1 :   builder = mwt_tariff_builder_new ();
      85                 :          1 :   mwt_tariff_builder_set_name (builder, "test-tariff");
      86                 :            : 
      87                 :            :   /* Period 1. */
      88                 :          2 :   g_autoptr(GDateTime) start1 = g_date_time_new_utc (2018, 1, 1, 0, 0, 0);
      89                 :          2 :   g_autoptr(GDateTime) end1 = g_date_time_new_utc (2018, 2, 1, 0, 0, 0);
      90                 :            : 
      91                 :          1 :   g_autoptr(MwtPeriod) period1 = NULL;
      92                 :          1 :   period1 = mwt_period_new (start1, end1, MWT_PERIOD_REPEAT_MONTH, 1,
      93                 :            :                             "capacity-limit", G_GUINT64_CONSTANT (2 * 1000 * 1000 * 1000),
      94                 :            :                             NULL);
      95                 :          1 :   mwt_tariff_builder_add_period (builder, period1);
      96                 :            : 
      97                 :            :   /* Period 2. */
      98                 :          2 :   g_autoptr(GDateTime) start2 = g_date_time_new_utc (2018, 1, 6, 0, 0, 0);
      99                 :          2 :   g_autoptr(GDateTime) end2 = g_date_time_new_utc (2018, 1, 8, 0, 0, 0);
     100                 :            : 
     101                 :          1 :   g_autoptr(MwtPeriod) period2 = NULL;
     102                 :          1 :   period2 = mwt_period_new (start2, end2, MWT_PERIOD_REPEAT_WEEK, 1,
     103                 :            :                             "capacity-limit", G_MAXUINT64,
     104                 :            :                             NULL);
     105                 :          1 :   mwt_tariff_builder_add_period (builder, period2);
     106                 :            : 
     107                 :            :   /* Finish building the tariff and check its properties. */
     108                 :          1 :   g_autoptr(MwtTariff) tariff = NULL;
     109                 :          1 :   tariff = mwt_tariff_builder_get_tariff (builder);
     110         [ -  + ]:          1 :   g_assert_nonnull (tariff);
     111                 :            : 
     112         [ -  + ]:          1 :   g_assert_cmpstr (mwt_tariff_get_name (tariff), ==, "test-tariff");
     113                 :          1 :   GPtrArray *periods = mwt_tariff_get_periods (tariff);
     114         [ -  + ]:          1 :   g_assert_nonnull (periods);
     115         [ -  + ]:          1 :   g_assert_cmpuint (periods->len, ==, 2);
     116                 :            : 
     117                 :          1 :   MwtPeriod *period1_built = g_ptr_array_index (periods, 0);
     118                 :          1 :   assert_periods_equal (period1_built, period1);
     119                 :          1 :   MwtPeriod *period2_built = g_ptr_array_index (periods, 1);
     120                 :          1 :   assert_periods_equal (period2_built, period2);
     121                 :            : 
     122                 :          1 :   g_autoptr(GVariant) variant = NULL;
     123                 :          1 :   variant = mwt_tariff_builder_get_tariff_as_variant (builder);
     124         [ -  + ]:          1 :   g_assert_nonnull (variant);
     125         [ -  + ]:          1 :   g_assert_false (g_variant_is_floating (variant));
     126         [ -  + ]:          1 :   g_assert_true (g_variant_is_of_type (variant, G_VARIANT_TYPE ("(sqv)")));
     127                 :            : 
     128                 :          1 :   g_autoptr(GBytes) bytes = NULL;
     129                 :          1 :   bytes = mwt_tariff_builder_get_tariff_as_bytes (builder);
     130         [ -  + ]:          1 :   g_assert_nonnull (bytes);
     131         [ -  + ]:          1 :   g_assert_cmpuint (g_bytes_get_size (bytes), ==, 140);
     132                 :          1 : }
     133                 :            : 
     134                 :            : /* Test building a tariff with no periods. This should fail. */
     135                 :            : static void
     136                 :          1 : test_tariff_builder_empty (void)
     137                 :            : {
     138                 :          1 :   g_autoptr(MwtTariffBuilder) builder = NULL;
     139                 :            : 
     140                 :            :   /* Build the tariff. */
     141                 :          1 :   builder = mwt_tariff_builder_new ();
     142                 :          1 :   mwt_tariff_builder_set_name (builder, "test-tariff");
     143                 :            : 
     144                 :            :   /* Finish building the tariff and check its properties. */
     145                 :          1 :   g_autoptr(MwtTariff) tariff = NULL;
     146                 :          1 :   tariff = mwt_tariff_builder_get_tariff (builder);
     147         [ -  + ]:          1 :   g_assert_null (tariff);
     148         [ -  + ]:          1 :   g_assert_null (mwt_tariff_builder_get_tariff_as_variant (builder));
     149         [ -  + ]:          1 :   g_assert_null (mwt_tariff_builder_get_tariff_as_bytes (builder));
     150                 :          1 : }
     151                 :            : 
     152                 :            : int
     153                 :          1 : main (int    argc,
     154                 :            :       char **argv)
     155                 :            : {
     156                 :          1 :   setlocale (LC_ALL, "");
     157                 :          1 :   g_test_init (&argc, &argv, NULL);
     158                 :            : 
     159                 :          1 :   g_test_add_func ("/tariff-builder/reset/empty", test_tariff_builder_reset_empty);
     160                 :          1 :   g_test_add_func ("/tariff-builder/reset/partial", test_tariff_builder_reset_partial);
     161                 :          1 :   g_test_add_func ("/tariff-builder/simple", test_tariff_builder_simple);
     162                 :          1 :   g_test_add_func ("/tariff-builder/empty", test_tariff_builder_empty);
     163                 :            : 
     164                 :          1 :   return g_test_run ();
     165                 :            : }

Generated by: LCOV version 1.16