alloc: Add very basic test case for the alloc_a algorithm
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 88be652..a752067 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,6 +1,6 @@
 AM_CPPFLAGS = $(STD_DEFINES_AND_INCLUDES) $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGB_CFLAGS) $(LIBOSMOGSM_CFLAGS) -I$(top_srcdir)/src/
 
-check_PROGRAMS = rlcmac/RLCMACTest
+check_PROGRAMS = rlcmac/RLCMACTest alloc/AllocTest
 noinst_PROGRAMS = emu/pcu_emu
 
 rlcmac_RLCMACTest_SOURCES = rlcmac/RLCMACTest.cpp
@@ -9,6 +9,14 @@
 	$(LIBOSMOCORE_LIBS) \
 	$(COMMON_LA)
 
+alloc_AllocTest_SOURCES = alloc/AllocTest.cpp
+alloc_AllocTest_LDADD = \
+	$(top_builddir)/src/libgprs.la \
+	$(LIBOSMOGB_LIBS) \
+	$(LIBOSMOGSM_LIBS) \
+	$(LIBOSMOCORE_LIBS) \
+	$(COMMON_LA)
+
 emu_pcu_emu_SOURCES = emu/pcu_emu.cpp emu/test_replay_gprs_attach.cpp \
 	emu/openbsc_clone.c emu/openbsc_clone.h emu/gprs_tests.h \
 	emu/test_pdp_activation.cpp
@@ -40,7 +48,8 @@
 
 EXTRA_DIST = \
 	testsuite.at $(srcdir)/package.m4 $(TESTSUITE)	\
-	rlcmac/RLCMACTest.ok rlcmac/RLCMACTest.err
+	rlcmac/RLCMACTest.ok rlcmac/RLCMACTest.err \
+	alloc/AllocTest.ok alloc/AllocTest.err
 
 DISTCLEANFILES = atconfig
 
diff --git a/tests/alloc/AllocTest.cpp b/tests/alloc/AllocTest.cpp
new file mode 100644
index 0000000..3d58f50
--- /dev/null
+++ b/tests/alloc/AllocTest.cpp
@@ -0,0 +1,123 @@
+/* AllocTest.cpp
+ *
+ * Copyright (C) 2013 by Holger Hans Peter Freyther
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
+
+#include "gprs_rlcmac.h"
+#include "gprs_debug.h"
+#include "tbf.h"
+
+#include <string.h>
+#include <stdio.h>
+
+extern "C" {
+#include <osmocom/core/application.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
+}
+
+/* globals used by the code */
+void *tall_pcu_ctx;
+struct gprs_rlcmac_bts *gprs_rlcmac_bts;
+int16_t spoof_mnc = 0, spoof_mcc = 0;
+
+static void test_alloc_a(gprs_rlcmac_tbf_direction dir, const int count)
+{
+	int tfi;
+	uint8_t used_trx;
+	struct gprs_rlcmac_bts bts;
+	struct gprs_rlcmac_tbf *tbfs[33] = { 0, };
+
+	printf("Testing alloc_a direction(%d)\n", dir);
+
+	memset(&bts, 0, sizeof(bts));
+	bts.alloc_algorithm = alloc_algorithm_a;
+	gprs_rlcmac_bts = &bts;
+
+	struct gprs_rlcmac_trx *trx = &bts.trx[0];
+	trx->pdch[2].enable = 1;
+	trx->pdch[3].enable = 1;
+
+	/**
+	 * Currently alloc_a will only allocate from the first
+	 * PDCH and all possible usf's. We run out of usf's before
+	 * we are out of tfi's. Observe this and make sure that at
+	 * least this part is working okay.
+	 */
+	for (int i = 0; i < count; ++i) {
+		struct gprs_rlcmac_tbf *tbf;
+
+		tfi = tfi_find_free(&bts, dir, &used_trx, 0);
+		OSMO_ASSERT(tfi >= 0);
+		tbfs[i] = tbf_alloc(&bts, NULL, dir, tfi, used_trx, 0, 0);
+	}
+
+	/* Now check that there are still some TFIs */
+	tfi = tfi_find_free(&bts, dir, &used_trx, 0);
+	switch (dir) {
+	case GPRS_RLCMAC_UL_TBF:
+		OSMO_ASSERT(tfi >= 0);
+		break;
+	case GPRS_RLCMAC_DL_TBF:
+		OSMO_ASSERT(tfi < 0);
+		break;
+	}
+	OSMO_ASSERT(!tbf_alloc(&bts, NULL, dir, tfi, used_trx, 0, 0));
+
+	for (int i = 0; i < ARRAY_SIZE(tbfs); ++i)
+		if (tbfs[i])
+			tbf_free(tbfs[i]);
+
+	tfi = tfi_find_free(&bts, dir, &used_trx, 0);
+	OSMO_ASSERT(tfi >= 0);
+
+	tbfs[tfi] = tbf_alloc(&bts, NULL, dir, tfi, used_trx, 0, 0);
+	OSMO_ASSERT(tbfs[tfi]);
+	tbf_free(tbfs[tfi]);
+}
+
+static void test_alloc_a()
+{
+	test_alloc_a(GPRS_RLCMAC_DL_TBF, 32);
+	test_alloc_a(GPRS_RLCMAC_UL_TBF, 7);
+}
+
+int main(int argc, char **argv)
+{
+	tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
+	if (!tall_pcu_ctx)
+		abort();
+
+	msgb_set_talloc_ctx(tall_pcu_ctx);
+	osmo_init_logging(&gprs_log_info);
+	log_set_use_color(osmo_stderr_target, 0);
+	log_set_print_filename(osmo_stderr_target, 0);
+
+	test_alloc_a();
+	return EXIT_SUCCESS;
+}
+
+/*
+ * stubs that should not be reached
+ */
+extern "C" {
+void l1if_pdch_req() { abort(); }
+void l1if_connect_pdch() { abort(); }
+void l1if_close_pdch() { abort(); }
+void l1if_open_pdch() { abort(); }
+}
diff --git a/tests/alloc/AllocTest.err b/tests/alloc/AllocTest.err
new file mode 100644
index 0000000..3f285a4
--- /dev/null
+++ b/tests/alloc/AllocTest.err
@@ -0,0 +1,2 @@
+No TFI available.
+- Failed allocating TS=2, no USF available
diff --git a/tests/alloc/AllocTest.ok b/tests/alloc/AllocTest.ok
new file mode 100644
index 0000000..86a8c19
--- /dev/null
+++ b/tests/alloc/AllocTest.ok
@@ -0,0 +1,2 @@
+Testing alloc_a direction(0)
+Testing alloc_a direction(1)
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 824c274..cad8a68 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -8,3 +8,10 @@
 cat $abs_srcdir/rlcmac/RLCMACTest.err > experr
 AT_CHECK([$abs_top_builddir/tests/rlcmac/RLCMACTest], [0], [expout], [experr])
 AT_CLEANUP
+
+AT_SETUP([ts_alloc])
+AT_KEYWORDS([ts_alloc])
+cat $abs_srcdir/alloc/AllocTest.ok > expout
+cat $abs_srcdir/alloc/AllocTest.err > experr
+AT_CHECK([$abs_top_builddir/tests/alloc/AllocTest], [0], [expout], [experr])
+AT_CLEANUP