Skip to content

Commit

Permalink
[unit] Slightly rearranged unit test code. JB#48413
Browse files Browse the repository at this point in the history
Made test_alloc_data() and test_clone_data() available to all
unit tests.
  • Loading branch information
monich committed May 11, 2020
1 parent 601e807 commit 204a7c9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
14 changes: 12 additions & 2 deletions unit/common/test_common.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018-2019 Jolla Ltd.
* Copyright (C) 2018-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2018-2020 Jolla Ltd.
* Copyright (C) 2018-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -69,6 +69,16 @@ test_quit_later_n(

#define TEST_TIMEOUT_SEC (20)

/* Utilities */
GUtilData*
test_alloc_data(
const void* bytes,
guint len);

GUtilData*
test_clone_data(
const GUtilData* data);

/* Helper macros */

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
Expand Down
34 changes: 32 additions & 2 deletions unit/common/test_main.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018-2019 Jolla Ltd.
* Copyright (C) 2018-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2018-2020 Jolla Ltd.
* Copyright (C) 2018-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -34,6 +34,36 @@

#include <gutil_log.h>

GUtilData*
test_alloc_data(
const void* bytes,
guint len)
{
if (bytes) {
const gsize total = len + sizeof(GUtilData);
GUtilData* data = g_malloc(total);

if (len) {
void* contents = (void*)(data + 1);

data->bytes = contents;
data->size = len;
memcpy(contents, bytes, len);
} else {
memset(data, 0, sizeof(*data));
}
return data;
}
return NULL;
}

GUtilData*
test_clone_data(
const GUtilData* data)
{
return data ? test_alloc_data(data->bytes, data->size) : NULL;
}

gboolean
test_timeout_expired(
gpointer data)
Expand Down
32 changes: 0 additions & 32 deletions unit/core_tag_t4/test_core_tag_t4.c
Expand Up @@ -174,38 +174,6 @@ static const guint8 test_resp_read_ndef_2[] = {
0x90, 0x00 /* SW1|SW2 */
};

static
GUtilData*
test_alloc_data(
const void* bytes,
guint len)
{
if (bytes) {
const gsize total = len + sizeof(GUtilData);
GUtilData* data = g_malloc(total);

if (len) {
void* contents = (void*)(data + 1);

data->bytes = contents;
data->size = len;
memcpy(contents, bytes, len);
} else {
memset(data, 0, sizeof(*data));
}
return data;
}
return NULL;
}

static
GUtilData*
test_clone_data(
const GUtilData* data)
{
return data ? test_alloc_data(data->bytes, data->size) : NULL;
}

static
void
test_tag_quit_loop_cb(
Expand Down

0 comments on commit 204a7c9

Please sign in to comment.