From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:700:3204::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id 2BbpHS1J4mVzbQAAbAwnHQ (envelope-from ) for ; Fri, 01 Mar 2024 22:31:25 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id gLHRFS1J4mUuLAAAe85BDQ (envelope-from ) for ; Fri, 01 Mar 2024 22:31:25 +0100 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; none Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 46E98449A3 for ; Fri, 1 Mar 2024 22:31:25 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=K01rbreUPN2qNasUau+9hOxO1jvy7E0Yi2bnqvnuph4=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1709328685; v=1; b=IHiVtrWotZyJ8+VaVsa2bLyGG2Tw9GGEbEoib1TDyapgJFpJZJ9IUbFpM4SsYfiACTYtmraQ vOjem+A4rYxbopF1tpZLgd6dBkVl4CI9+KkhX1iheBXnxSCRpqKd2HXFY+c03STwq2Mpn8Yo7BM ldzYkCAebzUxWBLwdj776ipIty34MTsFN4QfvjvHxI3ylhH/31lpJQgE/egKpnoJE3HoZw8l69l V2lBl06Gp32xMSFZR6smdTs1Uwx/4vCFsbUEIKJfOyZXZIjiHO3ZgFr5CQE8aEA7RalttH9g4rG WGjVX7/H2twDTYJepJ/SxEJhne8t5Gk0/C/pVPt7wcEpA== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 146EC203F4 for ; Fri, 1 Mar 2024 21:31:25 +0000 (UTC) Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) by mail-a.sr.ht (Postfix) with ESMTPS id 7B7E2202B5 for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 1 Mar 2024 21:31:24 +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=1709328684; 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=pm6gfT5kFW8Uzt9D5ABOgeHxzAProYo/uS3sEaAVlcY=; b=19D0YcmxgPcjfEo1K1fQVzGutLW3MeF12d5yrdCIESov7epwnqoV+1nDKQdpBr/jdf/v8j IQ/hAvbWVbDKwtjf/G94+a1yn6IrXmUKki3VeHcppzyP4olV3fzFF5auVzNxQ7da+hFNkw IHqg5XuIASBAP/NVHxo6xoA5nlUapE8kO+WB/j5aULqmGySM+h8e0RSwecZDBUr266Mzc2 c83dtEPEJWEKHEALprWdjzIQTyekI6TgL9Wmh4J/Vfcvd4UDtDNT6NWfMRBVxqxBK1VC8w QU9LA/JLq1BNPzG0PRwdDMuqX2v5TJ1JmxRKdYRQeCbfz/v6phAC9htv/oMZxA== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v2 2/4] string_view: add string view conversion to uint32_t Date: Fri, 1 Mar 2024 23:24:07 +0100 Message-ID: <20240301223127.167341-3-johnny@johnnyrichard.com> In-Reply-To: <20240301223127.167341-1-johnny@johnnyrichard.com> References: <20240301223127.167341-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Sourcehut-Patchset-Status: UNKNOWN List-Unsubscribe: List-Subscribe: List-Archive: Archived-At: List-Post: List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht> Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht> X-Migadu-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 46E98449A3 X-Migadu-Scanner: mx11.migadu.com X-TUID: bu2EmncGdbNq Signed-off-by: Johnny Richard --- v2: add slice string view test. replace memset call with a regular index assignment src/string_view.c | 11 ++++++ src/string_view.h | 5 ++- tests/unit/string_view_test.c | 67 +++++++++++++++++++++++++++++++++++ 3 files changed, 82 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..084f417 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]; + ret[str.size + 1] = 0; + memcpy(ret, str.chars, str.size); + return atoi(ret); +} diff --git a/src/string_view.h b/src/string_view.h index 1adc5c0..d5d2e6c 100644 --- a/src/string_view.h +++ b/src/string_view.h @@ -19,6 +19,7 @@ #include #include +#include #define SV_FMT "%.*s" #define SV_ARG(sv) (int)(sv).size, (sv).chars @@ -30,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..1d8627f --- /dev/null +++ b/tests/unit/string_view_test.c @@ -0,0 +1,67 @@ +/* + * 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 +#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")); + + char *return_stmt = "return EXIT_SUCCESS;"; + + str = (string_view_t){ .chars = return_stmt + 7, .size = 12 }; + assert_true(string_view_eq_to_cstr(str, "EXIT_SUCCESS")); + + 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