aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmicli/test/test-helpers.c
blob: cfdfefdd9117e3d21d0be7d58d949b52b45e33c5 (plain)
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details:
 *
 * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org>
 */

#include <glib.h>
#include "qmicli-helpers.h"

static void
test_helpers_raw_printable_1 (void)
{
    GArray *array;
    gchar *printable;
    static guint8 buffer[8] = {
        0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
    };
    static const gchar *expected =
        "0F:\n"
        "50:\n"
        "EB:\n"
        "E2:\n"
        "B6:\n"
        "00:\n"
        "00:\n"
        "00\n";

    array = g_array_sized_new (FALSE, FALSE, 1, 8);
    g_array_insert_vals (array, 0, buffer, 8);

    printable = qmicli_get_raw_data_printable (array, 3, "");

    g_assert_cmpstr (printable, ==, expected);
    g_free (printable);
    g_array_unref (array);
}

static void
test_helpers_raw_printable_2 (void)
{
    GArray *array;
    gchar *printable;
    static guint8 buffer[8] = {
        0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
    };
    static const gchar *expected =
        "\t0F:50:\n"
        "\tEB:E2:\n"
        "\tB6:00:\n"
        "\t00:00\n";

    array = g_array_sized_new (FALSE, FALSE, 1, 8);
    g_array_insert_vals (array, 0, buffer, 8);

    /* When passing 7, we'll be really getting 6 (the closest lower multiple of 3) */
    printable = qmicli_get_raw_data_printable (array, 7, "\t");

    g_assert_cmpstr (printable, ==, expected);
    g_free (printable);
    g_array_unref (array);
}

static void
test_helpers_raw_printable_3 (void)
{
    GArray *array;
    gchar *printable;
    static guint8 buffer[8] = {
        0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
    };
    static const gchar *expected =
        "\t\t\t0F:50:EB:E2:\n"
        "\t\t\tB6:00:00:00\n";

    array = g_array_sized_new (FALSE, FALSE, 1, 8);
    g_array_insert_vals (array, 0, buffer, 8);

    printable = qmicli_get_raw_data_printable (array, 12, "\t\t\t");

    g_assert_cmpstr (printable, ==, expected);
    g_free (printable);
    g_array_unref (array);
}

static void
test_helpers_raw_printable_4 (void)
{
    GArray *array;
    gchar *printable;
    static guint8 buffer[8] = {
        0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
    };
    static const gchar *expected =
        "\t0F:50:EB:E2:B6:00:00:00\n";

    array = g_array_sized_new (FALSE, FALSE, 1, 8);
    g_array_insert_vals (array, 0, buffer, 8);

    printable = qmicli_get_raw_data_printable (array, 24, "\t");

    g_assert_cmpstr (printable, ==, expected);
    g_free (printable);
    g_array_unref (array);
}

static void
test_helpers_supported_messages_list (void)
{
    const guint8 bytearray[] = {
        0x03, 0x00, 0x00, 0xC0
    };
    const gchar *expected_str =
        "\t0x0000\n"  /*  0 dec */
        "\t0x0001\n"  /*  1 dec */
        "\t0x001E\n"  /* 30 dec */
        "\t0x001F\n"; /* 31 dec */
    gchar *str;

    str = qmicli_get_supported_messages_list (bytearray, G_N_ELEMENTS (bytearray));
    g_assert (str);
    g_assert_cmpstr (str, ==, expected_str);
    g_free (str);
}

static void
test_helpers_supported_messages_list_none (void)
{
    const gchar *expected_str = "\tnone\n";
    gchar *str;

    str = qmicli_get_supported_messages_list (NULL, 0);
    g_assert (str);
    g_assert_cmpstr (str, ==, expected_str);
    g_free (str);
}

int main (int argc, char **argv)
{
    g_test_init (&argc, &argv, NULL);

    g_test_add_func ("/qmicli/helpers/raw-printable/1",  test_helpers_raw_printable_1);
    g_test_add_func ("/qmicli/helpers/raw-printable/2",  test_helpers_raw_printable_2);
    g_test_add_func ("/qmicli/helpers/raw-printable/3",  test_helpers_raw_printable_3);
    g_test_add_func ("/qmicli/helpers/raw-printable/4",  test_helpers_raw_printable_4);

    g_test_add_func ("/qmicli/helpers/supported-message-list",      test_helpers_supported_messages_list);
    g_test_add_func ("/qmicli/helpers/supported-message-list/none", test_helpers_supported_messages_list_none);

    return g_test_run ();
}