// SPDX-FileCopyrightText: 2017 kriw <kotarou777775@gmail.com>
// SPDX-License-Identifier: LGPL-3.0-only

#include <rz_util.h>
#include "minunit.h"

static RNum *num;

bool test_rz_num_units() {
	char humansz[8];
	const struct {
		const char *expected_res;
		const char *message;
		ut64 num;
	} test_cases[] = {
		{ "0", "B", 0ULL },
		{ "512", "B", 512ULL },
		{ "1K", "K", 1ULL << 10 },
		{ "1M", "M", 1ULL << 20 },
		{ "1G", "G", 1ULL << 30 },
		{ "1T", "T", 1ULL << 40 },
		{ "1P", "P", 1ULL << 50 },
		{ "1E", "E", 1ULL << 60 },
		/* Decimal test */
		{ "1.0K", "K", 1025 },
		{ "994K", "K", 994 * (1ULL << 10) },
		{ "999K", "K", 999 * (1ULL << 10) },
		{ "1.0M", "M", 1025 * (1ULL << 10) },
		{ "1.5M", "M", 1536 * (1ULL << 10) },
		{ "1.9M", "M", 1996 * (1ULL << 10) },
		{ "2.0M", "M", 1997 * (1ULL << 10) },
		{ "2.0M", "M", 2047 * (1ULL << 10) },
		{ "2M", "M", 2048 * (1ULL << 10) },
		{ "2.0M", "M", 2099 * (1ULL << 10) },
		{ "2.1M", "M", 2100 * (1ULL << 10) },
		{ "9.9G", "G", 10188 * (1ULL << 20) },
		/* Biggest units */
		{ "82P", "P", 82 * (1ULL << 50) },
		{ "16E", "E", UT64_MAX }
	};
	size_t nitems = sizeof(test_cases) / sizeof(test_cases[0]);
	size_t i;
	for (i = 0; i < nitems; i++) {
		rz_num_units(humansz, sizeof(humansz), test_cases[i].num);
		mu_assert_streq(humansz, test_cases[i].expected_res, test_cases[i].message);
	}
	mu_end;
}

bool test_rz_num_minmax_swap_i() {
	int a = -1, b = 2;
	rz_num_minmax_swap_i(&a, &b);
	mu_assert_eq(a == -1 && b == 2, 1, "a < b -> a < b");
	a = 2, b = -1;
	rz_num_minmax_swap_i(&a, &b);
	mu_assert_eq(a == -1 && b == 2, 1, "b < a -> a < b");
	mu_end;
}

bool test_rz_num_minmax_swap() {
	ut64 a = 1, b = 2;
	rz_num_minmax_swap(&a, &b);
	mu_assert_eq(a == 1 && b == 2, 1, "a < b -> a < b");
	a = 2, b = 1;
	rz_num_minmax_swap(&a, &b);
	mu_assert_eq(a == 1 && b == 2, 1, "b < a -> a < b");
	mu_end;
}

bool test_rz_num_between() {
	mu_assert_eq(rz_num_between(num, "1 2 3"), 1, "1 <= 2 <= 3");
	mu_assert_eq(rz_num_between(num, "3 2 1"), 0, "3 <= 2 <= 1");
	mu_assert_eq(rz_num_between(num, "1 1 1"), 1, "1 <= 1 <= 1");
	mu_assert_eq(rz_num_between(num, "2 1 3"), 0, "2 <= 1 <= 3");
	mu_assert_eq(rz_num_between(num, "1 2 1+2"), 1, "1 <= 2 <= 1+2");
	mu_assert_eq(rz_num_between(num, "2 3 1+2+3"), 1, "2 <= 3 <= 1+2+3");
	mu_assert_eq(rz_num_between(num, "1+2 2 1+1"), 0, "1+2 <= 2 <= 1+1");
	mu_assert_eq(rz_num_between(num, "1 + 2 2 1 + 1"), 0, "1 + 2 <= 2 <= 1 + 1");
	mu_end;
}

bool test_rz_num_str_len() {
	mu_assert_eq(rz_num_str_len("1"), 1, "\"1\"");
	mu_assert_eq(rz_num_str_len("1+1"), 3, "\"1+1\"");
	mu_assert_eq(rz_num_str_len("1 + 1"), 5, "\"1 + 1\"");
	mu_assert_eq(rz_num_str_len("1 + 1 "), 5, "\"1 + 1 \"");
	mu_assert_eq(rz_num_str_len("1 + 1  "), 5, "\"1 + 1  \"");
	mu_assert_eq(rz_num_str_len("1 + 1 1"), 5, "\"1 + 1 1\"");
	mu_assert_eq(rz_num_str_len("1 + 1 1 + 1"), 5, "\"1 + 1 1 + 1\"");
	mu_assert_eq(rz_num_str_len("1 + (1 + 1) 1"), 11, "\"1 + (1 + 1) 1\"");
	mu_assert_eq(rz_num_str_len("1 + (1 + (1 + 1)) 1"), 17, "\"1 + (1 + (1 + 1)) 1\"");
	mu_assert_eq(rz_num_str_len("1+(1+(1+1)) 1"), 11, "\"1+(1+(1+1)) 1\"");
	mu_assert_eq(rz_num_str_len("(1 + 1) + (1 + 1) 1"), 17, "\"(1 + 1) + (1 + 1) 1\"");
	mu_assert_eq(rz_num_str_len("(1+1)+(1+1) 1"), 11, "\"(1+1)+(1+1) 1\"");
	mu_end;
}

bool test_rz_num_str_split() {
	char *str = malloc(0x20);
	strcpy(str, "1 1 + 2 1 + (2 + 3) 4 ");
	//expected "1\01 + 2\01 + (2 + 3)\04\0"
	int count = rz_num_str_split(str);
	mu_assert_eq(count, 4, "rz_num_str_split (str) == 4");
	mu_assert_streq(str + 0, "1", "1");
	mu_assert_streq(str + 2, "1 + 2", "1 + 2");
	mu_assert_streq(str + 8, "1 + (2 + 3)", "1 + (2 + 3)");
	mu_assert_streq(str + 20, "4", "4");
	free(str);
	mu_end;
}

bool test_rz_num_str_split_list() {
	char *s;
	char *str = malloc(0x20);
	strcpy(str, "1 1 + 2 1 + (2 + 3) 4 ");
	//expected {"1", "1 + 2", "1 + (2 + 3)", "4"} as list
	RzList *list = rz_num_str_split_list(str);
	mu_assert_eq(rz_list_length(list), 4, "rz_list_length (list) == 4");
	s = (char *)rz_list_pop_head(list);
	mu_assert_streq(s, "1", "1");
	s = (char *)rz_list_pop_head(list);
	mu_assert_streq(s, "1 + 2", "1 + 2");
	s = (char *)rz_list_pop_head(list);
	mu_assert_streq(s, "1 + (2 + 3)", "1 + (2 + 3)");
	s = (char *)rz_list_pop_head(list);
	mu_assert_streq(s, "4", "4");
	free(str);
	rz_list_free(list);
	mu_end;
}

bool test_rz_num_align_delta() {
	ut64 d = rz_num_align_delta(0, 8);
	mu_assert_eq(d, 0, "align delta");
	d = rz_num_align_delta(3, 8);
	mu_assert_eq(d, 5, "align delta");
	d = rz_num_align_delta(0x10, 8);
	mu_assert_eq(d, 0, "align delta");
	d = rz_num_align_delta(0x11, 8);
	mu_assert_eq(d, 7, "align delta");
	d = rz_num_align_delta(0x42, 0);
	mu_assert_eq(d, 0, "align delta");
	mu_end;
}

bool all_tests() {
	mu_run_test(test_rz_num_units);
	mu_run_test(test_rz_num_minmax_swap_i);
	mu_run_test(test_rz_num_minmax_swap);
	mu_run_test(test_rz_num_between);
	mu_run_test(test_rz_num_str_len);
	mu_run_test(test_rz_num_str_split);
	mu_run_test(test_rz_num_str_split_list);
	mu_run_test(test_rz_num_align_delta);
	return tests_passed != tests_run;
}

int main(int argc, char **argv) {
	num = rz_num_new(NULL, NULL, NULL);
	return all_tests();
}
