mirror of
https://gitlab.freedesktop.org/xorg/lib/libxcb-errors.git
synced 2025-12-20 14:00:04 +01:00
Test: Factor out string-comparision helper function
This function compares strings, handles NULL (almost) correctly, prints an error if a mismatch occurs and returns a suitable int result. Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
3a6db57743
commit
b82a03f403
1 changed files with 49 additions and 52 deletions
93
tests/test.c
93
tests/test.c
|
|
@ -24,94 +24,91 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "xcb_errors.h"
|
#include "xcb_errors.h"
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define SKIP 77
|
#define SKIP 77
|
||||||
|
|
||||||
|
static int check_strings(const char *expected, const char *actual, const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
if (expected == NULL && actual == NULL)
|
||||||
|
return 0;
|
||||||
|
if (expected != NULL && actual != NULL && strcmp(expected, actual) == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
va_start(ap, format);
|
||||||
|
vfprintf(stderr, format, ap);
|
||||||
|
va_end(ap);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int check_request(xcb_errors_context_t *ctx, uint8_t opcode, const char *expected)
|
static int check_request(xcb_errors_context_t *ctx, uint8_t opcode, const char *expected)
|
||||||
{
|
{
|
||||||
const char *actual = xcb_errors_get_name_for_major_code(ctx, opcode);
|
const char *actual = xcb_errors_get_name_for_major_code(ctx, opcode);
|
||||||
if (strcmp(actual, expected) != 0) {
|
return check_strings(expected, actual, "For opcode %d: Expected %s, got %s\n",
|
||||||
fprintf(stderr, "For opcode %d: Expected %s, got %s\n", opcode,
|
opcode, expected, actual);
|
||||||
expected, actual);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_error(xcb_errors_context_t *ctx, uint8_t error,
|
static int check_error(xcb_errors_context_t *ctx, uint8_t error,
|
||||||
const char *expected, const char *expected_extension)
|
const char *expected, const char *expected_extension)
|
||||||
{
|
{
|
||||||
const char *actual, *actual_extension, *tmp;
|
const char *actual, *actual_extension, *tmp;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
actual = xcb_errors_get_name_for_error(ctx, error, &actual_extension);
|
actual = xcb_errors_get_name_for_error(ctx, error, &actual_extension);
|
||||||
if (actual_extension != expected_extension &&
|
ret |= check_strings(expected_extension, actual_extension,
|
||||||
strcmp(actual_extension, expected_extension) != 0) {
|
"For error %d: Expected ext %s, got %s\n",
|
||||||
fprintf(stderr, "For error %d: Expected ext %s, got %s\n", error,
|
error, expected_extension, actual_extension);
|
||||||
expected_extension, actual_extension);
|
ret |= check_strings(expected, actual,
|
||||||
return 1;
|
"For error %d: Expected %s, got %s\n",
|
||||||
}
|
error, expected, actual);
|
||||||
if (strcmp(actual, expected) != 0) {
|
|
||||||
fprintf(stderr, "For error %d: Expected %s, got %s\n", error,
|
|
||||||
expected, actual);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
tmp = xcb_errors_get_name_for_error(ctx, error, NULL);
|
tmp = xcb_errors_get_name_for_error(ctx, error, NULL);
|
||||||
if (tmp != actual) {
|
ret |= check_strings(actual, tmp,
|
||||||
fprintf(stderr, "For error %d: Passing NULL made a difference: %s vs %s\n",
|
"For error %d: Passing NULL made a difference: %s vs %s\n",
|
||||||
error, actual, tmp);
|
error, actual, tmp);
|
||||||
return 1;
|
return ret;
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_event(xcb_errors_context_t *ctx, uint8_t event,
|
static int check_event(xcb_errors_context_t *ctx, uint8_t event,
|
||||||
const char *expected, const char *expected_extension)
|
const char *expected, const char *expected_extension)
|
||||||
{
|
{
|
||||||
const char *actual, *actual_extension, *tmp;
|
const char *actual, *actual_extension, *tmp;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
actual = xcb_errors_get_name_for_event(ctx, event, &actual_extension);
|
actual = xcb_errors_get_name_for_event(ctx, event, &actual_extension);
|
||||||
if (actual_extension != expected_extension &&
|
ret |= check_strings(expected_extension, actual_extension,
|
||||||
strcmp(actual_extension, expected_extension) != 0) {
|
"For event %d: Expected ext %s, got %s\n",
|
||||||
fprintf(stderr, "For event %d: Expected ext %s, got %s\n", event,
|
event, expected_extension, actual_extension);
|
||||||
expected_extension, actual_extension);
|
ret |= check_strings(expected, actual,
|
||||||
return 1;
|
"For event %d: Expected %s, got %s\n",
|
||||||
}
|
event, expected, actual);
|
||||||
if (strcmp(actual, expected) != 0) {
|
|
||||||
fprintf(stderr, "For event %d: Expected %s, got %s\n", event,
|
|
||||||
expected, actual);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
tmp = xcb_errors_get_name_for_event(ctx, event, NULL);
|
tmp = xcb_errors_get_name_for_event(ctx, event, NULL);
|
||||||
if (tmp != actual) {
|
ret |= check_strings(actual, tmp,
|
||||||
fprintf(stderr, "For event %d: Passing NULL made a difference: %s vs %s\n",
|
"For event %d: Passing NULL made a difference: %s vs %s\n",
|
||||||
event, actual, tmp);
|
event, actual, tmp);
|
||||||
return 1;
|
return ret;
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_xge_event(xcb_errors_context_t *ctx, uint8_t major_code,
|
static int check_xge_event(xcb_errors_context_t *ctx, uint8_t major_code,
|
||||||
uint16_t event_type, const char *expected)
|
uint16_t event_type, const char *expected)
|
||||||
{
|
{
|
||||||
const char *actual = xcb_errors_get_name_for_xge_event(ctx, major_code, event_type);
|
const char *actual = xcb_errors_get_name_for_xge_event(ctx, major_code, event_type);
|
||||||
if (actual != expected && (actual == NULL || expected == NULL || strcmp(actual, expected) != 0)) {
|
return check_strings(expected, actual,
|
||||||
fprintf(stderr, "For xge event (%d, %d): Expected %s, got %s\n",
|
"For xge event (%d, %d): Expected %s, got %s\n",
|
||||||
major_code, event_type, expected, actual);
|
major_code, event_type, expected, actual);
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_minor(xcb_errors_context_t *ctx, uint8_t major, uint16_t minor, const char *expected)
|
static int check_minor(xcb_errors_context_t *ctx, uint8_t major, uint16_t minor, const char *expected)
|
||||||
{
|
{
|
||||||
const char *actual = xcb_errors_get_name_for_minor_code(ctx, major, minor);
|
const char *actual = xcb_errors_get_name_for_minor_code(ctx, major, minor);
|
||||||
if (actual != expected && (actual == NULL || expected == NULL || strcmp(actual, expected) != 0)) {
|
return check_strings(expected, actual, "For minor (%d, %d): Expected %s, got %s\n",
|
||||||
fprintf(stderr, "For minor (%d, %d): Expected %s, got %s\n",
|
|
||||||
major, minor, expected, actual);
|
major, minor, expected, actual);
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int test_error_connection(void)
|
static int test_error_connection(void)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue