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 v2 2/4] string_view: add string view conversion to uint32_t
Date: Fri,  1 Mar 2024 23:24:07 +0100	[thread overview]
Message-ID: <20240301223127.167341-3-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240301223127.167341-1-johnny@johnnyrichard.com>

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


  parent reply	other threads:[~2024-03-01 21:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-01 22:24 [PATCH olang v2 0/4] create initial syntax analysis logic Johnny Richard
2024-03-01 22:24 ` [PATCH olang v2 1/4] string_view: add string view formatter for printf fmt Johnny Richard
2024-03-01 22:24 ` Johnny Richard [this message]
2024-03-01 22:24 ` [PATCH olang v2 3/4] lexer: add token lookahead capability Johnny Richard
2024-03-01 22:24 ` [PATCH olang v2 4/4] parser: create simplified parser for tiny AST Johnny Richard
2024-03-01 21:32   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-03-01 22:08   ` [PATCH olang v2 4/4] parser: create simplified parser for tiny AST Carlos Maniero

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=20240301223127.167341-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