From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mail-a.sr.ht; dkim=pass header.d=johnnyrichard.com header.i=@johnnyrichard.com Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [IPv6:2001:41d0:203:375::ad]) by mail-a.sr.ht (Postfix) with ESMTPS id A1FC22032B for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 28 Feb 2024 18:10:19 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1709143819; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Oh4PkvDaojk9Mby3MzLHei+/8988ka8sGF3JHwxA7aU=; b=fh5cF5xzBykRZstaFhEVTDwRszlLXGsVDDbqr2d7/NjI49TgvAJYDL7ukj2v/DhK1ftdwh OHsuEn4H3GcRSNjIyz4wDW1SxyGyZWf7vmaC++7CS3EnLrSdITAP0oGXyUsTf5Y+rAAfMW /GEwNIu/DbJM6Qmkzx/YsSxRg+U1K4VtK+iUxQZdksmT8yuwlHVRUWbw4qiRDtEqvFzYUG cUIcDgKoLAIJN+pTc2+/oWo8YJ0jco9wH/xmWZLOW+sPOLtZEAaKej2uqOsL5A4JYZES11 wE95h56V/CeUy59W82kPJeQZrX1UTM0516hNFLY+2wHV9//96hCUjEV/IPa89A== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 2/4] string_view: add string view conversion to uint32_t Date: Wed, 28 Feb 2024 20:04:19 +0100 Message-ID: <20240228190956.78191-3-johnny@johnnyrichard.com> In-Reply-To: <20240228190956.78191-1-johnny@johnnyrichard.com> References: <20240228190956.78191-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-TUID: UlNmyhgtED29 Signed-off-by: Johnny Richard --- src/string_view.c | 11 +++++++ src/string_view.h | 4 ++- tests/unit/string_view_test.c | 61 +++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/unit/string_view_test.c diff --git a/src/string_view.c b/src/string_view.c index 122eaa2..58bf197 100644 --- a/src/string_view.c +++ b/src/string_view.c @@ -17,6 +17,8 @@ #include "string_view.h" #include +#include +#include #include bool @@ -33,3 +35,12 @@ string_view_eq_to_cstr(string_view_t str, char *cstr) } return i == cstr_len; } + +uint32_t +string_view_to_u32(string_view_t str) +{ + char ret[str.size + 1]; + memset(ret, 0, str.size + 1); + memcpy(ret, str.chars, str.size); + return atoi(ret); +} diff --git a/src/string_view.h b/src/string_view.h index a212ab9..d5d2e6c 100644 --- a/src/string_view.h +++ b/src/string_view.h @@ -31,8 +31,10 @@ typedef struct string_view } string_view_t; -// TODO: missing unit test bool string_view_eq_to_cstr(string_view_t str, char *cstr); +uint32_t +string_view_to_u32(string_view_t str); + #endif /* STRING_VIEW_T */ diff --git a/tests/unit/string_view_test.c b/tests/unit/string_view_test.c new file mode 100644 index 0000000..b5d1fcf --- /dev/null +++ b/tests/unit/string_view_test.c @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2024 olang maintainers + * + * 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 3 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, see . + */ +#define MUNIT_ENABLE_ASSERT_ALIASES +#include "munit.h" +#include "string_view.h" + +#include + +static MunitResult +string_view_eq_to_cstr_test(const MunitParameter params[], void *user_data_or_fixture) +{ + char *name = "John Doe"; + + string_view_t str = { .chars = name, .size = strlen(name) }; + + assert_true(string_view_eq_to_cstr(str, "John Doe")); + assert_false(string_view_eq_to_cstr(str, "Doe")); + + return MUNIT_OK; +} + +static MunitResult +string_view_to_u32_test(const MunitParameter params[], void *user_data_or_fixture) +{ + char *number = "69"; + + string_view_t str = { .chars = number, .size = strlen(number) }; + + assert_uint32(string_view_to_u32(str), ==, 69); + + return MUNIT_OK; +} + +static MunitTest tests[] = { + { "/eq_to_cstr_test", string_view_eq_to_cstr_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { "/to_u32_test", string_view_to_u32_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } +}; + +static const MunitSuite suite = { "/string_view", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE }; + +int +main(int argc, char *argv[]) +{ + return munit_suite_main(&suite, NULL, argc, argv); + return EXIT_SUCCESS; +} -- 2.43.2