| 1 |
/* |
| 2 |
* Copyright (c) 2023, Oracle and/or its affiliates. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
|
| 26 |
#include "../src/rgb.c" |
| 27 |
#include <glib.h> |
| 28 |
|
| 29 |
/* |
| 30 |
* xpmReadRgbNames - reads a rgb text file |
| 31 |
*/ |
| 32 |
|
| 33 |
struct rgbData { |
| 34 |
int r, g, b; |
| 35 |
const char *name; |
| 36 |
}; |
| 37 |
|
| 38 |
/* Changes here must match those in rgb.txt file */ |
| 39 |
static const struct rgbData testdata[] = { |
| 40 |
{ 255, 255, 255, "white" }, |
| 41 |
{ 0, 0, 0, "black" }, |
| 42 |
{ 255, 0, 0, "red" }, |
| 43 |
{ 0, 255, 0, "green" }, |
| 44 |
{ 0, 0, 255, "blue" }, |
| 45 |
{ 0, 50, 98, "berkeleyblue" }, /* names get lowercased */ |
| 46 |
{ 253, 181, 21, "californiagold" } |
| 47 |
}; |
| 48 |
#define NUM_RGB (sizeof(testdata) / sizeof(testdata[0])) |
| 49 |
|
| 50 |
static void |
| 51 |
test_xpmReadRgbNames(void) |
| 52 |
{ |
| 53 |
const gchar *filename; |
| 54 |
xpmRgbName rgbn[MAX_RGBNAMES]; |
| 55 |
int rgbn_max; |
| 56 |
|
| 57 |
/* Verify NULL is returned if file can't be read */ |
| 58 |
rgbn_max = xpmReadRgbNames("non-existent-file.txt", rgbn); |
| 59 |
g_assert_cmpint(rgbn_max, ==, 0); |
| 60 |
|
| 61 |
/* Verify our test file is read properly & contains expected data */ |
| 62 |
filename = g_test_get_filename(G_TEST_DIST, "rgb.txt", NULL); |
| 63 |
rgbn_max = xpmReadRgbNames(filename, rgbn); |
| 64 |
g_assert_cmpint(rgbn_max, ==, NUM_RGB); |
| 65 |
|
| 66 |
for (unsigned int i = 0; i < NUM_RGB; i++) { |
| 67 |
int r = testdata[i].r * 257; |
| 68 |
int g = testdata[i].g * 257; |
| 69 |
int b = testdata[i].b * 257; |
| 70 |
char *name = xpmGetRgbName(rgbn, rgbn_max, r, g, b); |
| 71 |
|
| 72 |
g_assert_cmpstr(name, ==, testdata[i].name); |
| 73 |
} |
| 74 |
|
| 75 |
g_assert_null(xpmGetRgbName(rgbn, rgbn_max, 11, 11, 11)); |
| 76 |
|
| 77 |
xpmFreeRgbNames(rgbn, rgbn_max); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
int |
| 82 |
main(int argc, char** argv) |
| 83 |
{ |
| 84 |
g_test_init(&argc, &argv, NULL); |
| 85 |
g_test_bug_base(PACKAGE_BUGREPORT); |
| 86 |
|
| 87 |
g_test_add_func("/rgb/xpmReadRgbNames", |
| 88 |
test_xpmReadRgbNames); |
| 89 |
|
| 90 |
return g_test_run(); |
| 91 |
} |