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
|
/*
* Copyright 2015 Canonical Limited
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* See the included COPYING file for more information.
*
* Author: Allison Ryan Lortie <desrt@desrt.ca>
*/
#include <glib.h>
typedef struct
{
gboolean success;
guint64 c, a, b;
} Case;
static void
test_checked_guint_add (void)
{
static const Case cases[] = {
/* success c = a + b */
{ TRUE, 0, 0, 0 },
{ TRUE, G_MAXINT, G_MAXINT, 0 },
{ TRUE, G_MAXINT, 0, G_MAXINT },
{ TRUE, G_MAXUINT, G_MAXUINT, 0 },
{ TRUE, G_MAXUINT, 0, G_MAXUINT },
{ TRUE, G_MAXUINT - 1, G_MAXINT, G_MAXINT },
{ FALSE, 0, G_MAXUINT, 1 },
{ FALSE, 0, 1, G_MAXUINT },
{ FALSE, 0, G_MAXUINT, G_MAXUINT }
};
guint i;
for (i = 0; i < G_N_ELEMENTS (cases); i++)
{
guint result;
g_assert_cmpuint (cases[i].success, ==, g_uint_checked_add (&result, cases[i].a, cases[i].b));
if (cases[i].success)
g_assert_cmpuint (cases[i].c, ==, result);
}
}
static void
test_checked_guint_mul (void)
{
static const Case cases[] = {
/* success c = a * b */
{ TRUE, 0, 0, 0 },
{ TRUE, 0, G_MAXINT, 0 },
{ TRUE, G_MAXINT, G_MAXINT, 1 },
{ TRUE, 0, G_MAXUINT, 0 },
{ TRUE, G_MAXUINT, G_MAXUINT, 1 },
{ TRUE, 2 * (guint) G_MAXINT, 2, G_MAXINT },
{ TRUE, 2 * (guint) G_MAXINT, G_MAXINT, 2 },
{ FALSE, 0, 3, G_MAXINT },
{ FALSE, 0, G_MAXINT, 3 }
};
guint i;
for (i = 0; i < G_N_ELEMENTS (cases); i++)
{
guint result;
g_assert_cmpuint (cases[i].success, ==, g_uint_checked_mul (&result, cases[i].a, cases[i].b));
if (cases[i].success)
g_assert_cmpuint (cases[i].c, ==, result);
}
}
static void
test_checked_guint64_add (void)
{
static const Case cases[] = {
/* success c = a + b */
{ TRUE, 0, 0, 0 },
{ TRUE, G_MAXINT64, G_MAXINT64, 0 },
{ TRUE, G_MAXINT64, 0, G_MAXINT64 },
{ TRUE, G_MAXUINT64, G_MAXUINT64, 0 },
{ TRUE, G_MAXUINT64, 0, G_MAXUINT64 },
{ TRUE, G_MAXUINT64 - 1, G_MAXINT64, G_MAXINT64 },
{ FALSE, 0, G_MAXUINT64, 1 },
{ FALSE, 0, 1, G_MAXUINT64 },
{ FALSE, 0, G_MAXUINT64, G_MAXUINT64 }
};
guint i;
for (i = 0; i < G_N_ELEMENTS (cases); i++)
{
guint64 result;
g_assert_cmpuint (cases[i].success, ==, g_uint64_checked_add (&result, cases[i].a, cases[i].b));
if (cases[i].success)
g_assert_cmpuint (cases[i].c, ==, result);
}
}
static void
test_checked_guint64_mul (void)
{
static const Case cases[] = {
/* success c = a * b */
{ TRUE, 0, 0, 0 },
{ TRUE, 0, G_MAXINT64, 0 },
{ TRUE, G_MAXINT64, G_MAXINT64, 1 },
{ TRUE, 0, G_MAXUINT64, 0 },
{ TRUE, G_MAXUINT64, G_MAXUINT64, 1 },
{ TRUE, 2 * (guint64) G_MAXINT64, 2, G_MAXINT64 },
{ TRUE, 2 * (guint64) G_MAXINT64, G_MAXINT64, 2 },
{ FALSE, 0, 3, G_MAXINT64 },
{ FALSE, 0, G_MAXINT64, 3 }
};
guint i;
for (i = 0; i < G_N_ELEMENTS (cases); i++)
{
guint64 result;
g_assert_cmpuint (cases[i].success, ==, g_uint64_checked_mul (&result, cases[i].a, cases[i].b));
if (cases[i].success)
g_assert_cmpuint (cases[i].c, ==, result);
}
}
static void
test_checked_gsize_add (void)
{
static const Case cases[] = {
/* success c = a + b */
{ TRUE, 0, 0, 0 },
{ TRUE, G_MAXSSIZE, G_MAXSSIZE, 0 },
{ TRUE, G_MAXSSIZE, 0, G_MAXSSIZE },
{ TRUE, G_MAXSIZE, G_MAXSIZE, 0 },
{ TRUE, G_MAXSIZE, 0, G_MAXSIZE },
{ TRUE, G_MAXSIZE - 1, G_MAXSSIZE, G_MAXSSIZE },
{ FALSE, 0, G_MAXSIZE, 1 },
{ FALSE, 0, 1, G_MAXSIZE },
{ FALSE, 0, G_MAXSIZE, G_MAXSIZE }
};
guint i;
for (i = 0; i < G_N_ELEMENTS (cases); i++)
{
gsize result;
g_assert_cmpuint (cases[i].success, ==, g_size_checked_add (&result, cases[i].a, cases[i].b));
if (cases[i].success)
g_assert_cmpuint (cases[i].c, ==, result);
}
}
static void
test_checked_gsize_mul (void)
{
static const Case cases[] = {
/* success c = a * b */
{ TRUE, 0, 0, 0 },
{ TRUE, 0, G_MAXSSIZE, 0 },
{ TRUE, G_MAXSSIZE, G_MAXSSIZE, 1 },
{ TRUE, 0, G_MAXSIZE, 0 },
{ TRUE, G_MAXSIZE, G_MAXSIZE, 1 },
{ TRUE, 2 * (gsize) G_MAXSSIZE, 2, G_MAXSSIZE },
{ TRUE, 2 * (gsize) G_MAXSSIZE, G_MAXSSIZE, 2 },
{ FALSE, 0, 3, G_MAXSSIZE },
{ FALSE, 0, G_MAXSSIZE, 3 }
};
guint i;
for (i = 0; i < G_N_ELEMENTS (cases); i++)
{
gsize result;
g_assert_cmpuint (cases[i].success, ==, g_size_checked_mul (&result, cases[i].a, cases[i].b));
if (cases[i].success)
g_assert_cmpuint (cases[i].c, ==, result);
}
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/glib/checked-math/guint-add", test_checked_guint_add);
g_test_add_func ("/glib/checked-math/guint-mul", test_checked_guint_mul);
g_test_add_func ("/glib/checked-math/guint64-add", test_checked_guint64_add);
g_test_add_func ("/glib/checked-math/guint64-mul", test_checked_guint64_mul);
g_test_add_func ("/glib/checked-math/gsize-add", test_checked_gsize_add);
g_test_add_func ("/glib/checked-math/gsize-mul", test_checked_gsize_mul);
return g_test_run ();
}
|