public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1 2/4] string_view: add string view conversion to uint32_t
Date: Wed, 28 Feb 2024 20:04:19 +0100	[thread overview]
Message-ID: <20240228190956.78191-3-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240228190956.78191-1-johnny@johnnyrichard.com>

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 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 <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
 #include <string.h>
 
 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 <https://www.gnu.org/licenses/>.
+ */
+#define MUNIT_ENABLE_ASSERT_ALIASES
+#include "munit.h"
+#include "string_view.h"
+
+#include <string.h>
+
+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


  parent reply	other threads:[~2024-02-28 18:10 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-28 19:04 [PATCH olang v1 0/4] create initial syntax analysis logic Johnny Richard
2024-02-28 19:04 ` [PATCH olang v1 1/4] string_view: add string view formatter for printf fmt Johnny Richard
2024-02-28 19:04 ` Johnny Richard [this message]
2024-02-29 15:16   ` [PATCH olang v1 2/4] string_view: add string view conversion to uint32_t Carlos Maniero
2024-02-29 22:40     ` Johnny Richard
2024-02-28 19:04 ` [PATCH olang v1 3/4] lexer: add token lookahead capability Johnny Richard
2024-02-28 19:04 ` [PATCH olang v1 4/4] parser: create simplified parser for tiny AST Johnny Richard
2024-02-28 18:11   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-03-01  3:34   ` [PATCH olang v1 4/4] parser: create simplified parser for tiny AST Carlos Maniero
2024-03-01 22:23     ` Johnny Richard
2024-03-01 22:33 ` [PATCH olang v1 0/4] create initial syntax analysis logic Johnny Richard

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=20240228190956.78191-3-johnny@johnnyrichard.com \
    --to=johnny@johnnyrichard.com \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox