git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
blob 1ca327b3c75ad217b43b3e57142e3c8f6ec5c62f 3335 bytes (raw)
name: reftable/block_test.c 	 # note: path name is non-authoritative(*)

  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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
 
/*
Copyright 2020 Google LLC

Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/

#include "block.h"

#include "system.h"

#include "blocksource.h"
#include "basics.h"
#include "constants.h"
#include "record.h"
#include "reftable.h"
#include "test_framework.h"
#include "reftable-tests.h"

struct binsearch_args {
	int key;
	int *arr;
};

static int binsearch_func(size_t i, void *void_args)
{
	struct binsearch_args *args = (struct binsearch_args *)void_args;

	return args->key < args->arr[i];
}

static void test_binsearch(void)
{
	int arr[] = { 2, 4, 6, 8, 10 };
	size_t sz = ARRAY_SIZE(arr);
	struct binsearch_args args = {
		.arr = arr,
	};

	int i = 0;
	for (i = 1; i < 11; i++) {
		int res;
		args.key = i;
		res = binsearch(sz, &binsearch_func, &args);

		if (res < sz) {
			assert(args.key < arr[res]);
			if (res > 0) {
				assert(args.key >= arr[res - 1]);
			}
		} else {
			assert(args.key == 10 || args.key == 11);
		}
	}
}

static void test_block_read_write(void)
{
	const int header_off = 21; /* random */
	char *names[30];
	const int N = ARRAY_SIZE(names);
	const int block_size = 1024;
	struct reftable_block block = { 0 };
	struct block_writer bw = {
		.last_key = STRBUF_INIT,
	};
	struct reftable_ref_record ref = { 0 };
	struct reftable_record rec = { 0 };
	int i = 0;
	int n;
	struct block_reader br = { 0 };
	struct block_iter it = { .last_key = STRBUF_INIT };
	int j = 0;
	struct strbuf want = STRBUF_INIT;

	block.data = reftable_calloc(block_size);
	block.len = block_size;
	block.source = malloc_block_source();
	block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
			  header_off, hash_size(SHA1_ID));
	reftable_record_from_ref(&rec, &ref);

	for (i = 0; i < N; i++) {
		char name[100];
		uint8_t hash[SHA1_SIZE];
		snprintf(name, sizeof(name), "branch%02d", i);
		memset(hash, i, sizeof(hash));

		ref.refname = name;
		ref.value = hash;
		names[i] = xstrdup(name);
		n = block_writer_add(&bw, &rec);
		ref.refname = NULL;
		ref.value = NULL;
		assert(n == 0);
	}

	n = block_writer_finish(&bw);
	assert(n > 0);

	block_writer_clear(&bw);

	block_reader_init(&br, &block, header_off, block_size, SHA1_SIZE);

	block_reader_start(&br, &it);

	while (1) {
		int r = block_iter_next(&it, &rec);
		assert(r >= 0);
		if (r > 0) {
			break;
		}
		assert_streq(names[j], ref.refname);
		j++;
	}

	reftable_record_clear(&rec);
	block_iter_close(&it);

	for (i = 0; i < N; i++) {
		struct block_iter it = { .last_key = STRBUF_INIT };
		strbuf_reset(&want);
		strbuf_addstr(&want, names[i]);

		n = block_reader_seek(&br, &it, &want);
		assert(n == 0);

		n = block_iter_next(&it, &rec);
		assert(n == 0);

		assert_streq(names[i], ref.refname);

		want.len--;
		n = block_reader_seek(&br, &it, &want);
		assert(n == 0);

		n = block_iter_next(&it, &rec);
		assert(n == 0);
		assert_streq(names[10 * (i / 10)], ref.refname);

		block_iter_close(&it);
	}

	reftable_record_clear(&rec);
	reftable_block_done(&br.block);
	strbuf_release(&want);
	for (i = 0; i < N; i++) {
		reftable_free(names[i]);
	}
}

int block_test_main(int argc, const char *argv[])
{
	add_test_case("binsearch", &test_binsearch);
	add_test_case("block_read_write", &test_block_read_write);
	return test_main(argc, argv);
}

debug log:

solving 1ca327b3c7 ...
found 1ca327b3c7 in https://public-inbox.org/git/1ba8e3eb3045b3ab6462febeaa8b90dc14910098.1600283416.git.gitgitgadget@gmail.com/

applying [1/1] https://public-inbox.org/git/1ba8e3eb3045b3ab6462febeaa8b90dc14910098.1600283416.git.gitgitgadget@gmail.com/
diff --git a/reftable/block_test.c b/reftable/block_test.c
new file mode 100644
index 0000000000..1ca327b3c7

Checking patch reftable/block_test.c...
Applied patch reftable/block_test.c cleanly.

index at:
100644 1ca327b3c75ad217b43b3e57142e3c8f6ec5c62f	reftable/block_test.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).