public inbox for iwd@lists.linux.dev
 help / color / mirror / Atom feed
From: James Prestwood <prestwoj@gmail.com>
To: iwd@lists.linux.dev
Cc: James Prestwood <prestwoj@gmail.com>
Subject: [PATCH 2/8] unit: add linear mapping test
Date: Tue, 19 Nov 2024 06:24:24 -0800	[thread overview]
Message-ID: <20241119142430.323074-2-prestwoj@gmail.com> (raw)
In-Reply-To: <20241119142430.323074-1-prestwoj@gmail.com>

---
 unit/test-util.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)

diff --git a/unit/test-util.c b/unit/test-util.c
index 0bfd1587..214d4819 100644
--- a/unit/test-util.c
+++ b/unit/test-util.c
@@ -184,6 +184,75 @@ static void ip_prefix_test(const void *data)
 	}
 }
 
+struct linear_map_case {
+	double value;
+	double a_start;
+	double a_end;
+	double b_start;
+	double b_end;
+	double expected;
+
+	bool fails : 1;
+};
+
+struct linear_map_case cases[] = {
+	/* Value out of range */
+	{
+		.value = 100,
+		.a_start = 1,
+		.a_end = 3,
+		.b_start = 10,
+		.b_end = 12,
+		.fails = true,
+	},
+	/* Ascending start range */
+	{
+		.value = 1,
+		.a_start = 0,
+		.a_end = 2,
+		.b_start = 10,
+		.b_end = 12,
+		.expected = 11,
+	},
+	/* Descending start range */
+	{
+		.value = 1,
+		.a_start = 2,
+		.a_end = 0,
+		.b_start = 10,
+		.b_end = 12,
+		.expected = 11,
+	},
+	/* Negatives */
+	{
+		.value = -1,
+		.a_start = -2,
+		.a_end = 0,
+		.b_start = 0,
+		.b_end = 2,
+		.expected = 1,
+	},
+};
+
+static void test_linear_map(const void *data)
+{
+	double mapped;
+	unsigned int i;
+
+	for (i = 0; i < L_ARRAY_SIZE(cases); i++) {
+		struct linear_map_case *c = &cases[i];
+		bool ret = util_linear_map(c->value, c->a_start, c->a_end,
+						c->b_start, c->b_end, &mapped);
+		if (c->fails) {
+			assert(ret == false);
+			continue;
+		}
+
+		assert(ret);
+		assert(mapped == c->expected);
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -192,6 +261,7 @@ int main(int argc, char *argv[])
 	l_test_add("/util/get_domain/", get_domain_test, NULL);
 	l_test_add("/util/get_username/", get_username_test, NULL);
 	l_test_add("/util/ip_prefix/", ip_prefix_test, NULL);
+	l_test_add("/util/test_linear_map", test_linear_map, NULL);
 
 	return l_test_run();
 }
-- 
2.34.1


  reply	other threads:[~2024-11-19 14:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-19 14:24 [PATCH 1/8] util: add util_linear_map James Prestwood
2024-11-19 14:24 ` James Prestwood [this message]
2024-11-19 14:24 ` [PATCH 3/8] util: add util_exponential_decay James Prestwood
2024-11-20 17:52   ` Denis Kenzior
2024-11-19 14:24 ` [PATCH 4/8] network: use util_exponential_decay James Prestwood
2024-11-19 14:24 ` [PATCH 5/8] scan: parse station count from BSS load IE James Prestwood
2024-11-19 14:24 ` [PATCH 6/8] scan: add ranking modifiers for utilization/station count James Prestwood
2024-11-19 14:24 ` [PATCH 7/8] doc: document [Rank].HighUtilization/StationCount thresholds James Prestwood
2024-11-19 14:24 ` [PATCH 8/8] station: print client count in scan results James Prestwood

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241119142430.323074-2-prestwoj@gmail.com \
    --to=prestwoj@gmail.com \
    --cc=iwd@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox