Welcome to mirror list, hosted at ThFree Co, Russian Federation.

zbx_variant_compare.c « zbxcommon « libs « tests - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 840376de7faa20a2a426f91056497c080f75ed9d (plain)
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
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

#include "zbxmocktest.h"
#include "zbxmockdata.h"
#include "zbxmockassert.h"
#include "zbxmockutil.h"

#include "common.h"
#include "zbxvariant.h"

static unsigned int	hex2num(char c)
{
	int	res;

	if (c >= 'a')
		res = c - 'a' + 10;	/* a-f */
	else if (c >= 'A')
		res = c - 'A' + 10;	/* A-F */
	else
		res = c - '0';		/* 0-9 */

	return (unsigned int)res;
}

static void	mock_read_variant(const char *path, zbx_variant_t *variant)
{
	zbx_mock_handle_t	hvalue;
	const char		*type;
	const char		*value;

	hvalue = zbx_mock_get_parameter_handle(path);
	type = zbx_mock_get_object_member_string(hvalue, "type");

	if (0 == strcmp(type, "ZBX_VARIANT_NONE"))
	{
		zbx_variant_set_none(variant);
		return;
	}

	value = zbx_mock_get_object_member_string(hvalue, "value");

	if (0 == strcmp(type, "ZBX_VARIANT_STR"))
	{
		zbx_variant_set_str(variant, zbx_strdup(NULL, value));
		return;
	}

	if (0 == strcmp(type, "ZBX_VARIANT_DBL"))
	{
		zbx_variant_set_dbl(variant, atof(value));
		return;
	}

	if (0 == strcmp(type, "ZBX_VARIANT_UI64"))
	{
		zbx_uint64_t	value_ui64;

		if (SUCCEED != is_uint64(value, &value_ui64))
			fail_msg("Cannot convert value %s to uint64", value);

		zbx_variant_set_ui64(variant, value_ui64);
		return;
	}

	if (0 == strcmp(type, "ZBX_VARIANT_BIN"))
	{
		zbx_uint32_t		i, size;
		char			*data;
		const char		*ptr = value;

		size = (strlen(value) + 1) / 3;
		data = (0 != size ? zbx_malloc(NULL, size) : NULL);

		for (i = 0; i < size; i++)
		{
			data[i] = hex2num(*ptr++) << 4;
			data[i] |= hex2num(*ptr++);
			ptr++;
		}
		zbx_variant_set_bin(variant, zbx_variant_data_bin_create(data, size));
		zbx_free(data);
		return;
	}

	fail_msg("Invalid variant type: %s", type);
}

void	zbx_mock_test_entry(void **state)
{
	zbx_variant_t	value1, value2;
	int		ret;
	const char	*returned_result;

	ZBX_UNUSED(state);

	ZBX_DOUBLE_EPSILON = 0.000001;

	mock_read_variant("in.value1", &value1);
	mock_read_variant("in.value2", &value2);

	ret = zbx_variant_compare(&value1, &value2);

	if (ret < 0)
		returned_result = "less";
	else if (ret > 0)
		returned_result = "greater";
	else
		returned_result = "equal";

	zbx_mock_assert_str_eq("zbx_variant_compare() return", zbx_mock_get_parameter_string("out.return"),
			returned_result);

	zbx_variant_clear(&value1);
	zbx_variant_clear(&value2);
}