5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen/***
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen This file is part of systemd
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen Copyright 2015 Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen systemd is free software; you can redistribute it and/or modify it
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen under the terms of the GNU Lesser General Public License as published by
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen (at your option) any later version.
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen systemd is distributed in the hope that it will be useful, but
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen Lesser General Public License for more details.
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen You should have received a copy of the GNU Lesser General Public License
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen***/
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen#include "bitmap.h"
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersenint main(int argc, const char *argv[]) {
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen _cleanup_bitmap_free_ Bitmap *b = NULL, *b2 = NULL;
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen Iterator it;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen unsigned n = (unsigned) -1, i = 0;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen b = bitmap_new();
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(b);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_ensure_allocated(&b) == 0);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen bitmap_free(b);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen b = NULL;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_ensure_allocated(&b) == 0);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isset(b, 0) == false);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isset(b, 1) == false);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isset(b, 256) == false);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isclear(b) == true);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_set(b, 0) == 0);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isset(b, 0) == true);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isclear(b) == false);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen bitmap_unset(b, 0);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isset(b, 0) == false);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isclear(b) == true);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_set(b, 1) == 0);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isset(b, 1) == true);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isclear(b) == false);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen bitmap_unset(b, 1);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isset(b, 1) == false);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isclear(b) == true);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_set(b, 256) == 0);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isset(b, 256) == true);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isclear(b) == false);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen bitmap_unset(b, 256);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isset(b, 256) == false);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isclear(b) == true);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
cdf6f5ae0465a8fb25d2afc758911985cc542a07Tom Gundersen assert_se(bitmap_set(b, 32) == 0);
cdf6f5ae0465a8fb25d2afc758911985cc542a07Tom Gundersen bitmap_unset(b, 0);
cdf6f5ae0465a8fb25d2afc758911985cc542a07Tom Gundersen assert_se(bitmap_isset(b, 32) == true);
cdf6f5ae0465a8fb25d2afc758911985cc542a07Tom Gundersen bitmap_unset(b, 32);
cdf6f5ae0465a8fb25d2afc758911985cc542a07Tom Gundersen
cdf6f5ae0465a8fb25d2afc758911985cc542a07Tom Gundersen BITMAP_FOREACH(n, NULL, it)
cdf6f5ae0465a8fb25d2afc758911985cc542a07Tom Gundersen assert_not_reached("NULL bitmap");
cdf6f5ae0465a8fb25d2afc758911985cc542a07Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_set(b, 0) == 0);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_set(b, 1) == 0);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_set(b, 256) == 0);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen BITMAP_FOREACH(n, b, it) {
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(n == i);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen if (i == 0)
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen i = 1;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen else if (i == 1)
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen i = 256;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen else if (i == 256)
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen i = (unsigned) -1;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen }
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(i == (unsigned) -1);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen i = 0;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen BITMAP_FOREACH(n, b, it) {
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(n == i);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen if (i == 0)
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen i = 1;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen else if (i == 1)
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen i = 256;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen else if (i == 256)
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen i = (unsigned) -1;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen }
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(i == (unsigned) -1);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen bitmap_clear(b);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_isclear(b) == true);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen assert_se(bitmap_set(b, (unsigned) -1) == -ERANGE);
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen bitmap_free(b);
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen b = NULL;
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen assert_se(bitmap_ensure_allocated(&b) == 0);
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen assert_se(bitmap_ensure_allocated(&b2) == 0);
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen assert_se(bitmap_equal(b, b2));
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen assert_se(bitmap_set(b, 0) == 0);
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen bitmap_unset(b, 0);
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen assert_se(bitmap_equal(b, b2));
d5fa81995849cb263ecfcd0aa6ab661360d9213eMartin Mikkelsen
951c3eefacedcdbdb2cebf245f043aa3e81fb483Martin Mikkelsen assert_se(bitmap_set(b, 1) == 0);
951c3eefacedcdbdb2cebf245f043aa3e81fb483Martin Mikkelsen bitmap_clear(b);
951c3eefacedcdbdb2cebf245f043aa3e81fb483Martin Mikkelsen assert_se(bitmap_equal(b, b2));
951c3eefacedcdbdb2cebf245f043aa3e81fb483Martin Mikkelsen
951c3eefacedcdbdb2cebf245f043aa3e81fb483Martin Mikkelsen assert_se(bitmap_set(b, 0) == 0);
951c3eefacedcdbdb2cebf245f043aa3e81fb483Martin Mikkelsen assert_se(bitmap_set(b2, 0) == 0);
951c3eefacedcdbdb2cebf245f043aa3e81fb483Martin Mikkelsen assert_se(bitmap_equal(b, b2));
951c3eefacedcdbdb2cebf245f043aa3e81fb483Martin Mikkelsen
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen return 0;
5ffa42cb8028833440040c2e240e0d788f11c112Tom Gundersen}