#include "config.h"
#include <stdio.h>
#include <assert.h>
#include <common/wireaddr.h>
#include <common/bigsize.h>
#include <common/channel_id.h>
#include <common/setup.h>
#include <common/utils.h>
#include <common/node_id.h>
#include <ccan/read_write_all/read_write_all.h>
#include <wire/onion_wiregen.h>

#include "../flow.c"
#include "../mcf.c"

/* AUTOGENERATED MOCKS START */
/* AUTOGENERATED MOCKS END */

int main(int argc, char *argv[])
{
	bool dual;
	u32 part;
	int chandir;
	u32 chanidx;

	common_setup(argv[0]);

	for (int i = 0; i < 32; i++) {
		for (int j = 0; j < 32; j++) {
			for (int k = 0; k < 32; k++) {
				struct arc a, a2;

				a.idx = (1U << i) | (1U << j) | (1U << k);
				arc_to_parts(a, &chanidx, &chandir, &part, &dual);
				a2 = arc_from_parts(chanidx, chandir, part, dual);
				assert(a.idx == a2.idx);
			}
		}
	}

	/* Test all chanidx */
	for (int i = 0; i < (1U << ARC_CHANIDX_BITS); i++) {
		struct arc a = arc_from_parts(i, chandir, part, dual);

		arc_to_parts(a, &chanidx, NULL, NULL, NULL);
		assert(chanidx == i);
	}

	/* Test both chandir */
	for (int i = 0; i < 2; i++) {
		struct arc a = arc_from_parts(chanidx, i, part, dual);

		arc_to_parts(a, NULL, &chandir, NULL, NULL);
		assert(chandir == i);
	}

	/* Test all parts */
	for (int i = 0; i < CHANNEL_PARTS; i++) {
		struct arc a = arc_from_parts(chanidx, chandir, i, dual);

		arc_to_parts(a, NULL, NULL, &part, NULL);
		assert(part == i);
	}

	/* Test both dual */
	for (int i = 0; i < 2; i++) {
		struct arc a = arc_from_parts(chanidx, chandir, part, i);

		arc_to_parts(a, NULL, NULL, NULL, &dual);
		assert(dual == i);

		assert(arc_is_dual(a) == dual);

		a = arc_dual(a);
		arc_to_parts(a, NULL, NULL, NULL, &dual);
		assert(dual == !i);
		assert(arc_is_dual(a) == dual);
	}

	common_shutdown();
}

