1
0
mirror of https://github.com/profanity-im/profanity.git synced 2025-01-03 14:57:42 -05:00

Added function to get next available window number, and tests

This commit is contained in:
James Booth 2013-08-27 22:02:23 +01:00
parent 77dcd04b10
commit 42c3a1c1f7
4 changed files with 364 additions and 0 deletions

View File

@ -399,6 +399,70 @@ get_unique_id(void)
return result;
}
int
cmp_win_num(gconstpointer a, gconstpointer b)
{
int real_a = GPOINTER_TO_INT(a);
int real_b = GPOINTER_TO_INT(b);
if (real_a == 0) {
real_a = 10;
}
if (real_b == 0) {
real_b = 10;
}
if (real_a < real_b) {
return -1;
} else if (real_a == real_b) {
return 0;
} else {
return 1;
}
}
int
get_next_available_win_num(GSList *used)
{
int result = 0;
used = g_slist_sort(used, cmp_win_num);
// only console used
if (g_slist_length(used) == 1) {
return 2;
} else {
int last_num = 1;
GSList *curr = used;
// skip console
curr = g_slist_next(curr);
while (curr != NULL) {
int curr_num = GPOINTER_TO_INT(curr->data);
if (((last_num != 9) && ((last_num + 1) != curr_num)) ||
((last_num == 9) && (curr_num != 0))) {
g_slist_free(curr);
result = last_num + 1;
if (result == 10) {
result = 0;
}
return (result);
} else {
last_num = curr_num;
if (last_num == 0) {
last_num = 10;
}
}
curr = g_slist_next(curr);
}
result = last_num + 1;
if (result == 10) {
result = 0;
}
return result;
}
}
static size_t
_data_callback(void *ptr, size_t size, size_t nmemb, void *data)
{

View File

@ -91,4 +91,7 @@ contact_presence_t contact_presence_from_resource_presence(resource_presence_t r
char * get_unique_id(void);
int cmp_win_num(gconstpointer a, gconstpointer b);
int get_next_available_win_num(GSList *used);
#endif

View File

@ -32,6 +32,7 @@
#include <ncurses.h>
#endif
#include "common.h"
#include "config/theme.h"
#include "ui/ui.h"
#include "ui/window.h"

View File

@ -146,6 +146,283 @@ void replace_when_new_null(void)
assert_string_equals("hello", result);
}
void compare_win_nums_less(void)
{
gconstpointer a = GINT_TO_POINTER(2);
gconstpointer b = GINT_TO_POINTER(3);
int result = cmp_win_num(a, b);
assert_true(result < 0);
}
void compare_win_nums_equal(void)
{
gconstpointer a = GINT_TO_POINTER(5);
gconstpointer b = GINT_TO_POINTER(5);
int result = cmp_win_num(a, b);
assert_true(result == 0);
}
void compare_win_nums_greater(void)
{
gconstpointer a = GINT_TO_POINTER(7);
gconstpointer b = GINT_TO_POINTER(6);
int result = cmp_win_num(a, b);
assert_true(result > 0);
}
void compare_0s_equal(void)
{
gconstpointer a = GINT_TO_POINTER(0);
gconstpointer b = GINT_TO_POINTER(0);
int result = cmp_win_num(a, b);
assert_true(result == 0);
}
void compare_0_greater_than_1(void)
{
gconstpointer a = GINT_TO_POINTER(0);
gconstpointer b = GINT_TO_POINTER(1);
int result = cmp_win_num(a, b);
assert_true(result > 0);
}
void compare_1_less_than_0(void)
{
gconstpointer a = GINT_TO_POINTER(1);
gconstpointer b = GINT_TO_POINTER(0);
int result = cmp_win_num(a, b);
assert_true(result < 0);
}
void compare_0_less_than_11(void)
{
gconstpointer a = GINT_TO_POINTER(0);
gconstpointer b = GINT_TO_POINTER(11);
int result = cmp_win_num(a, b);
assert_true(result < 0);
}
void compare_11_greater_than_0(void)
{
gconstpointer a = GINT_TO_POINTER(11);
gconstpointer b = GINT_TO_POINTER(0);
int result = cmp_win_num(a, b);
assert_true(result > 0);
}
void compare_0_greater_than_9(void)
{
gconstpointer a = GINT_TO_POINTER(0);
gconstpointer b = GINT_TO_POINTER(9);
int result = cmp_win_num(a, b);
assert_true(result > 0);
}
void compare_9_less_than_0(void)
{
gconstpointer a = GINT_TO_POINTER(9);
gconstpointer b = GINT_TO_POINTER(0);
int result = cmp_win_num(a, b);
assert_true(result < 0);
}
void next_available_when_only_console(void)
{
GSList *used = NULL;
used = g_slist_append(used, GINT_TO_POINTER(1));
int result = get_next_available_win_num(used);
assert_int_equals(2, result);
}
void next_available_3_at_end(void)
{
GSList *used = NULL;
used = g_slist_append(used, GINT_TO_POINTER(1));
used = g_slist_append(used, GINT_TO_POINTER(2));
int result = get_next_available_win_num(used);
assert_int_equals(3, result);
}
void next_available_9_at_end(void)
{
GSList *used = NULL;
used = g_slist_append(used, GINT_TO_POINTER(1));
used = g_slist_append(used, GINT_TO_POINTER(2));
used = g_slist_append(used, GINT_TO_POINTER(3));
used = g_slist_append(used, GINT_TO_POINTER(4));
used = g_slist_append(used, GINT_TO_POINTER(5));
used = g_slist_append(used, GINT_TO_POINTER(6));
used = g_slist_append(used, GINT_TO_POINTER(7));
used = g_slist_append(used, GINT_TO_POINTER(8));
int result = get_next_available_win_num(used);
assert_int_equals(9, result);
}
void next_available_0_at_end(void)
{
GSList *used = NULL;
used = g_slist_append(used, GINT_TO_POINTER(1));
used = g_slist_append(used, GINT_TO_POINTER(2));
used = g_slist_append(used, GINT_TO_POINTER(3));
used = g_slist_append(used, GINT_TO_POINTER(4));
used = g_slist_append(used, GINT_TO_POINTER(5));
used = g_slist_append(used, GINT_TO_POINTER(6));
used = g_slist_append(used, GINT_TO_POINTER(7));
used = g_slist_append(used, GINT_TO_POINTER(8));
used = g_slist_append(used, GINT_TO_POINTER(9));
int result = get_next_available_win_num(used);
assert_int_equals(0, result);
}
void next_available_2_in_first_gap(void)
{
GSList *used = NULL;
used = g_slist_append(used, GINT_TO_POINTER(1));
used = g_slist_append(used, GINT_TO_POINTER(3));
used = g_slist_append(used, GINT_TO_POINTER(4));
used = g_slist_append(used, GINT_TO_POINTER(5));
used = g_slist_append(used, GINT_TO_POINTER(9));
used = g_slist_append(used, GINT_TO_POINTER(0));
int result = get_next_available_win_num(used);
assert_int_equals(2, result);
}
void next_available_9_in_first_gap(void)
{
GSList *used = NULL;
used = g_slist_append(used, GINT_TO_POINTER(1));
used = g_slist_append(used, GINT_TO_POINTER(2));
used = g_slist_append(used, GINT_TO_POINTER(3));
used = g_slist_append(used, GINT_TO_POINTER(4));
used = g_slist_append(used, GINT_TO_POINTER(5));
used = g_slist_append(used, GINT_TO_POINTER(6));
used = g_slist_append(used, GINT_TO_POINTER(7));
used = g_slist_append(used, GINT_TO_POINTER(8));
used = g_slist_append(used, GINT_TO_POINTER(0));
used = g_slist_append(used, GINT_TO_POINTER(11));
used = g_slist_append(used, GINT_TO_POINTER(12));
used = g_slist_append(used, GINT_TO_POINTER(13));
used = g_slist_append(used, GINT_TO_POINTER(20));
int result = get_next_available_win_num(used);
assert_int_equals(9, result);
}
void next_available_0_in_first_gap(void)
{
GSList *used = NULL;
used = g_slist_append(used, GINT_TO_POINTER(1));
used = g_slist_append(used, GINT_TO_POINTER(2));
used = g_slist_append(used, GINT_TO_POINTER(3));
used = g_slist_append(used, GINT_TO_POINTER(4));
used = g_slist_append(used, GINT_TO_POINTER(5));
used = g_slist_append(used, GINT_TO_POINTER(6));
used = g_slist_append(used, GINT_TO_POINTER(7));
used = g_slist_append(used, GINT_TO_POINTER(8));
used = g_slist_append(used, GINT_TO_POINTER(9));
used = g_slist_append(used, GINT_TO_POINTER(11));
used = g_slist_append(used, GINT_TO_POINTER(12));
used = g_slist_append(used, GINT_TO_POINTER(13));
used = g_slist_append(used, GINT_TO_POINTER(20));
int result = get_next_available_win_num(used);
assert_int_equals(0, result);
}
void next_available_11_in_first_gap(void)
{
GSList *used = NULL;
used = g_slist_append(used, GINT_TO_POINTER(1));
used = g_slist_append(used, GINT_TO_POINTER(2));
used = g_slist_append(used, GINT_TO_POINTER(3));
used = g_slist_append(used, GINT_TO_POINTER(4));
used = g_slist_append(used, GINT_TO_POINTER(5));
used = g_slist_append(used, GINT_TO_POINTER(6));
used = g_slist_append(used, GINT_TO_POINTER(7));
used = g_slist_append(used, GINT_TO_POINTER(8));
used = g_slist_append(used, GINT_TO_POINTER(9));
used = g_slist_append(used, GINT_TO_POINTER(0));
used = g_slist_append(used, GINT_TO_POINTER(12));
used = g_slist_append(used, GINT_TO_POINTER(13));
used = g_slist_append(used, GINT_TO_POINTER(20));
int result = get_next_available_win_num(used);
assert_int_equals(11, result);
}
void next_available_24_first_big_gap(void)
{
GSList *used = NULL;
used = g_slist_append(used, GINT_TO_POINTER(1));
used = g_slist_append(used, GINT_TO_POINTER(2));
used = g_slist_append(used, GINT_TO_POINTER(3));
used = g_slist_append(used, GINT_TO_POINTER(4));
used = g_slist_append(used, GINT_TO_POINTER(5));
used = g_slist_append(used, GINT_TO_POINTER(6));
used = g_slist_append(used, GINT_TO_POINTER(7));
used = g_slist_append(used, GINT_TO_POINTER(8));
used = g_slist_append(used, GINT_TO_POINTER(9));
used = g_slist_append(used, GINT_TO_POINTER(0));
used = g_slist_append(used, GINT_TO_POINTER(11));
used = g_slist_append(used, GINT_TO_POINTER(12));
used = g_slist_append(used, GINT_TO_POINTER(13));
used = g_slist_append(used, GINT_TO_POINTER(14));
used = g_slist_append(used, GINT_TO_POINTER(15));
used = g_slist_append(used, GINT_TO_POINTER(16));
used = g_slist_append(used, GINT_TO_POINTER(17));
used = g_slist_append(used, GINT_TO_POINTER(18));
used = g_slist_append(used, GINT_TO_POINTER(19));
used = g_slist_append(used, GINT_TO_POINTER(20));
used = g_slist_append(used, GINT_TO_POINTER(21));
used = g_slist_append(used, GINT_TO_POINTER(22));
used = g_slist_append(used, GINT_TO_POINTER(23));
used = g_slist_append(used, GINT_TO_POINTER(51));
used = g_slist_append(used, GINT_TO_POINTER(52));
used = g_slist_append(used, GINT_TO_POINTER(53));
used = g_slist_append(used, GINT_TO_POINTER(89));
used = g_slist_append(used, GINT_TO_POINTER(90));
used = g_slist_append(used, GINT_TO_POINTER(100));
used = g_slist_append(used, GINT_TO_POINTER(101));
used = g_slist_append(used, GINT_TO_POINTER(102));
int result = get_next_available_win_num(used);
assert_int_equals(24, result);
}
void register_common_tests(void)
{
TEST_MODULE("common tests");
@ -162,4 +439,23 @@ void register_common_tests(void)
TEST(replace_when_sub_null);
TEST(replace_when_new_empty);
TEST(replace_when_new_null);
TEST(compare_win_nums_less);
TEST(compare_win_nums_equal);
TEST(compare_win_nums_greater);
TEST(compare_0s_equal);
TEST(compare_0_greater_than_1);
TEST(compare_1_less_than_0);
TEST(compare_0_less_than_11);
TEST(compare_11_greater_than_0);
TEST(compare_0_greater_than_9);
TEST(compare_9_less_than_0);
TEST(next_available_when_only_console);
TEST(next_available_3_at_end);
TEST(next_available_9_at_end);
TEST(next_available_0_at_end);
TEST(next_available_2_in_first_gap);
TEST(next_available_9_in_first_gap);
TEST(next_available_0_in_first_gap);
TEST(next_available_11_in_first_gap);
TEST(next_available_24_first_big_gap);
}