diff mbox

[FFmpeg-devel] avutil: Improved test coverage for avstring.c

Message ID 1481973785-21290-1-git-send-email-thomastdt@googlemail.com
State Changes Requested
Headers show

Commit Message

Thomas Turner Dec. 17, 2016, 11:23 a.m. UTC
Signed-off-by: Thomas Turner <thomastdt@googlemail.com>
---
 libavutil/tests/avstring.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

Comments

Michael Niedermayer Dec. 17, 2016, 4:20 p.m. UTC | #1
On Sat, Dec 17, 2016 at 03:23:05AM -0800, Thomas Turner wrote:
> Signed-off-by: Thomas Turner <thomastdt@googlemail.com>
> ---
>  libavutil/tests/avstring.c | 25 ++++++++++++++++++++++++-
>  1 file changed, 24 insertions(+), 1 deletion(-)
> 
> diff --git a/libavutil/tests/avstring.c b/libavutil/tests/avstring.c
> index 1242b3f..1e22b4f 100644
> --- a/libavutil/tests/avstring.c
> +++ b/libavutil/tests/avstring.c
> @@ -25,7 +25,7 @@
>  int main(void)
>  {
>      int i;
> -    char *fullpath;
> +    char *fullpath, *ptr;
>      static const char * const strings[] = {
>          "''",
>          "",
> @@ -54,6 +54,8 @@ int main(void)
>          "'\\fo\\o:': blahblah",
>          "\\'fo\\o\\:':  foo  '  :blahblah"
>      };
> +    const char *haystack = "Education consists mainly in what we have unlearned.";
> +    const char * const needle[] = {"learned.", "unlearned.", "Unlearned"};
>  
>      printf("Testing av_get_token()\n");
>      for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
> @@ -79,5 +81,26 @@ int main(void)
>      TEST_APPEND_PATH_COMPONENT("path", "/comp", "path/comp");
>      TEST_APPEND_PATH_COMPONENT("path/", "/comp", "path/comp");
>      TEST_APPEND_PATH_COMPONENT("path/path2/", "/comp/comp2", "path/path2/comp/comp2");
> +
> +    /*Testing av_strnstr()*/
> +    #define TEST_STRNSTR(haystack, needle, hay_length, expected) \
> +        ptr = av_strnstr(haystack, needle, hay_length); \
> +        if (ptr != expected){ \
> +            printf("%s should equal: %s\n", ptr, expected); \
> +        }
> +    TEST_STRNSTR(haystack, needle [0], strlen(haystack), haystack+44);
> +    TEST_STRNSTR(haystack, needle [1], strlen(haystack), haystack+42);
> +    TEST_STRNSTR(haystack, needle [2], strlen(haystack), NULL       );
> +    TEST_STRNSTR(haystack, strings[1], strlen(haystack), haystack   );

this produces 2 warnings

libavutil/tests/avstring.c:93:5: warning: reading through null pointer (argument 3) [-Wformat]
libavutil/tests/avstring.c:93:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘void *’ [-Wformat]


> +
> +    /*Testing av_d2str()*/
> +    #define TEST_D2STR(value, expected) \
> +        ptr = av_d2str(value); \
> +        if(strcmp(ptr, expected) != 0){ \

> +            printf("av_d2str failed to convert: %lf to string: %s", (double)value, expected); \
                                                    ^^
%lf is the same as %f which is for double already. float gets
converted to double for functions like printf(). I imagine lf might
result in different formating but we dont use %lf in a print function
anywhere else
i would suggest to avoid adding "print %lf" as it doesnt test anything
we use

[...]
diff mbox

Patch

diff --git a/libavutil/tests/avstring.c b/libavutil/tests/avstring.c
index 1242b3f..1e22b4f 100644
--- a/libavutil/tests/avstring.c
+++ b/libavutil/tests/avstring.c
@@ -25,7 +25,7 @@ 
 int main(void)
 {
     int i;
-    char *fullpath;
+    char *fullpath, *ptr;
     static const char * const strings[] = {
         "''",
         "",
@@ -54,6 +54,8 @@  int main(void)
         "'\\fo\\o:': blahblah",
         "\\'fo\\o\\:':  foo  '  :blahblah"
     };
+    const char *haystack = "Education consists mainly in what we have unlearned.";
+    const char * const needle[] = {"learned.", "unlearned.", "Unlearned"};
 
     printf("Testing av_get_token()\n");
     for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
@@ -79,5 +81,26 @@  int main(void)
     TEST_APPEND_PATH_COMPONENT("path", "/comp", "path/comp");
     TEST_APPEND_PATH_COMPONENT("path/", "/comp", "path/comp");
     TEST_APPEND_PATH_COMPONENT("path/path2/", "/comp/comp2", "path/path2/comp/comp2");
+
+    /*Testing av_strnstr()*/
+    #define TEST_STRNSTR(haystack, needle, hay_length, expected) \
+        ptr = av_strnstr(haystack, needle, hay_length); \
+        if (ptr != expected){ \
+            printf("%s should equal: %s\n", ptr, expected); \
+        }
+    TEST_STRNSTR(haystack, needle [0], strlen(haystack), haystack+44);
+    TEST_STRNSTR(haystack, needle [1], strlen(haystack), haystack+42);
+    TEST_STRNSTR(haystack, needle [2], strlen(haystack), NULL       );
+    TEST_STRNSTR(haystack, strings[1], strlen(haystack), haystack   );
+
+    /*Testing av_d2str()*/
+    #define TEST_D2STR(value, expected) \
+        ptr = av_d2str(value); \
+        if(strcmp(ptr, expected) != 0){ \
+            printf("av_d2str failed to convert: %lf to string: %s", (double)value, expected); \
+        }
+    TEST_D2STR(0         ,  "0.000000");
+    TEST_D2STR(-1.2333234, "-1.233323");
+    TEST_D2STR(-1.2333237, "-1.233324");
     return 0;
 }