/* Copyright (c) 2014, Daniel Martí
* Copyright (c) 2014-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define CONSDIFF_PRIVATE
#include "core/or/or.h"
#include "test/test.h"
#include "feature/dircommon/consdiff.h"
#include "lib/memarea/memarea.h"
#include "test/log_test_helpers.h"
#define tt_str_eq_line(a,b) \
tt_assert(line_str_eq((b),(a)))
static void
test_consdiff_smartlist_slice(void *arg)
{
smartlist_t *sl = smartlist_new();
smartlist_slice_t *sls;
int items[6] = {0,0,0,0,0,0};
/* Create a regular smartlist. */
(void)arg;
smartlist_add(sl, &items[1]);
smartlist_add(sl, &items[2]);
smartlist_add(sl, &items[3]);
smartlist_add(sl, &items[4]);
smartlist_add(sl, &items[5]);
/* See if the slice was done correctly. */
sls = smartlist_slice(sl, 2, 5);
tt_ptr_op(sl, OP_EQ, sls->list);
tt_ptr_op(&items[3], OP_EQ, smartlist_get(sls->list, sls->offset));
tt_ptr_op(&items[5], OP_EQ,
smartlist_get(sls->list, sls->offset + (sls->len-1)));
tor_free(sls);
/* See that using -1 as the end does get to the last element. */
sls = smartlist_slice(sl, 2, -1);
tt_ptr_op(sl, OP_EQ, sls->list);
tt_ptr_op(&items[3], OP_EQ, smartlist_get(sls->list, sls->offset));
tt_ptr_op(&items[5], OP_EQ,
smartlist_get(sls->list, sls->offset + (sls->len-1)));
done:
tor_free(sls);
smartlist_free(sl);
}
static void
test_consdiff_smartlist_slice_string_pos(void *arg)
{
smartlist_t *sl = smartlist_new();
smartlist_slice_t *sls;
memarea_t *area = memarea_new();
/* Create a regular smartlist. */
(void)arg;
consensus_split_lines(sl, "a\nd\nc\na\nb\n", area);
/* See that smartlist_slice_string_pos respects the bounds of the slice. */
sls = smartlist_slice(sl, 2, 5);
cdline_t a_line = { "a", 1 };
tt_int_op(3, OP_EQ, smartlist_slice_string_pos(sls, &a_line));
cdline_t d_line = { "d", 1 };
tt_int_op(-1, OP_EQ, smartlist_slice_string_pos(sls, &d_line));
done:
tor_free(sls);
smartlist_free(sl);
memarea_drop_all(area);
}
stati