summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bx/tests/sort_test.cpp
blob: a46a80b64f8d7be1b7974ce86e7e8f507aa1e9c7 (plain) (blame)
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
/*
 * Copyright 2010-2019 Branimir Karadzic. All rights reserved.
 * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
 */

#include "test.h"
#include <bx/sort.h>
#include <bx/string.h>
#include <bx/rng.h>

TEST_CASE("quickSort", "")
{
	const char* str[] =
	{
		"jabuka",
		"kruska",
		"malina",
		"jagoda",
	};

	bx::quickSort(str, BX_COUNTOF(str), sizeof(void*)
		, [](const void* _lhs, const void* _rhs)
		{
			const char* lhs = *(const char**)_lhs;
			const char* rhs = *(const char**)_rhs;
			return bx::strCmp(lhs, rhs);
		});

	REQUIRE(0 == bx::strCmp(str[0], "jabuka") );
	REQUIRE(0 == bx::strCmp(str[1], "jagoda") );
	REQUIRE(0 == bx::strCmp(str[2], "kruska") );
	REQUIRE(0 == bx::strCmp(str[3], "malina") );

	int8_t byte[128];
	bx::RngMwc rng;
	for (uint32_t ii = 0; ii < BX_COUNTOF(byte); ++ii)
	{
		byte[ii] = rng.gen()&0xff;
	}

	bx::quickSort(byte, BX_COUNTOF(byte), 1
		, [](const void* _lhs, const void* _rhs)
		{
			int8_t lhs = *(const int8_t*)_lhs;
			int8_t rhs = *(const int8_t*)_rhs;
			return lhs - rhs;
		});

	for (uint32_t ii = 1; ii < BX_COUNTOF(byte); ++ii)
	{
		REQUIRE(byte[ii-1] <= byte[ii]);
	}
}