Richard W.M. Jones
2015-Jul-01 13:28 UTC
[Libguestfs] [PATCH 1/2] utils: Add a test of the guestfs_int_drive_name function.
This function didn't have a unit test before. --- src/test-utils.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/test-utils.c b/src/test-utils.c index 8e1491f..c5e2f08 100644 --- a/src/test-utils.c +++ b/src/test-utils.c @@ -1,5 +1,5 @@ /* libguestfs - * Copyright (C) 2014 Red Hat Inc. + * Copyright (C) 2014-2015 Red Hat Inc. * * 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 @@ -162,6 +162,32 @@ test_validate_guid (void) assert (guestfs_int_validate_guid ("21EC2020-3AEA-1069-A2DD-08002B30309D") == 1); } +/* Test guestfs_int_drive_name. */ +static void +test_drive_name (void) +{ + char s[10]; + + guestfs_int_drive_name (0, s); + assert (STREQ (s, "a")); + guestfs_int_drive_name (25, s); + assert (STREQ (s, "z")); + guestfs_int_drive_name (26, s); + assert (STREQ (s, "aa")); + guestfs_int_drive_name (27, s); + assert (STREQ (s, "ab")); + guestfs_int_drive_name (51, s); + assert (STREQ (s, "az")); + guestfs_int_drive_name (52, s); + assert (STREQ (s, "ba")); + guestfs_int_drive_name (701, s); + assert (STREQ (s, "zz")); + guestfs_int_drive_name (702, s); + assert (STREQ (s, "aaa")); + guestfs_int_drive_name (18277, s); + assert (STREQ (s, "zzz")); +} + int main (int argc, char *argv[]) { @@ -169,6 +195,7 @@ main (int argc, char *argv[]) test_concat (); test_join (); test_validate_guid (); + test_drive_name (); exit (EXIT_SUCCESS); } -- 2.3.1
Richard W.M. Jones
2015-Jul-01 13:28 UTC
[Libguestfs] [PATCH 2/2] utils: Add guestfs_int_drive_index and unit test.
Add guestfs_int_drive_index which does basically the opposite of guestfs_int_drive_name. This commit also includes a unit test. --- src/guestfs-internal-frontend.h | 1 + src/test-utils.c | 16 ++++++++++++++++ src/utils.c | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/guestfs-internal-frontend.h b/src/guestfs-internal-frontend.h index 9322201..e3f0db6 100644 --- a/src/guestfs-internal-frontend.h +++ b/src/guestfs-internal-frontend.h @@ -105,6 +105,7 @@ extern char **guestfs_int_split_string (char sep, const char *); extern char *guestfs_int_exit_status_to_string (int status, const char *cmd_name, char *buffer, size_t buflen); extern int guestfs_int_random_string (char *ret, size_t len); extern char *guestfs_int_drive_name (size_t index, char *ret); +extern ssize_t guestfs_int_drive_index (const char *); extern int guestfs_int_is_true (const char *str); /* These functions are used internally by the CLEANUP_* macros. diff --git a/src/test-utils.c b/src/test-utils.c index c5e2f08..9fd5a29 100644 --- a/src/test-utils.c +++ b/src/test-utils.c @@ -188,6 +188,21 @@ test_drive_name (void) assert (STREQ (s, "zzz")); } +/* Test guestfs_int_drive_index. */ +static void +test_drive_index (void) +{ + assert (guestfs_int_drive_index ("a") == 0); + assert (guestfs_int_drive_index ("z") == 25); + assert (guestfs_int_drive_index ("aa") == 26); + assert (guestfs_int_drive_index ("ab") == 27); + assert (guestfs_int_drive_index ("az") == 51); + assert (guestfs_int_drive_index ("ba") == 52); + assert (guestfs_int_drive_index ("zz") == 701); + assert (guestfs_int_drive_index ("aaa") == 702); + assert (guestfs_int_drive_index ("zzz") == 18277); +} + int main (int argc, char *argv[]) { @@ -196,6 +211,7 @@ main (int argc, char *argv[]) test_join (); test_validate_guid (); test_drive_name (); + test_drive_index (); exit (EXIT_SUCCESS); } diff --git a/src/utils.c b/src/utils.c index 7cee16e..e9ec38e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -271,6 +271,27 @@ guestfs_int_drive_name (size_t index, char *ret) return ret; } +/* The opposite of guestfs_int_drive_name. Take a string like "ab" + * and return the index (eg 27). Note that you must remove any prefix + * such as "hd", "sd" etc, or any partition number before calling the + * function. + */ +ssize_t +guestfs_int_drive_index (const char *name) +{ + ssize_t r = 0; + + while (*name) { + if (*name >= 'a' && *name <= 'z') + r = 26*r + (*name - 'a' + 1); + else + return -1; + name++; + } + + return r-1; +} + /* Similar to Tcl_GetBoolean. */ int guestfs_int_is_true (const char *str) -- 2.3.1
Possibly Parallel Threads
- [PATCH v2 0/2] utils: Add guestfs_int_drive_index and unit test.
- [PATCH 1/9] v2v: Stable bus and slot numbers for removable drives (RHBZ#1238053).
- [PATCH] daemon: reorder internal static libs to fix linking
- [PATCH] v2v: Rename OVF module to Create_ovf.
- [PATCH v2 0/4] daemon: Translate device names if Linux device is unstable (RHBZ#1804207).