LCOV - code coverage report
Current view: top level - src/libavutil/tests - parseutils.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 72 76 94.7 %
Date: 2017-01-22 02:20:28 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #define TEST
      20             : #include "libavutil/parseutils.c"
      21             : 
      22             : #include <stdint.h>
      23             : #include <stdio.h>
      24             : 
      25             : #include "libavutil/common.h"
      26             : #include "libavutil/log.h"
      27             : #include "libavutil/rational.h"
      28             : 
      29             : static uint32_t randomv = MKTAG('L','A','V','U');
      30             : 
      31           3 : static uint32_t av_get_random_seed_deterministic(void)
      32             : {
      33           3 :     return randomv = randomv * 1664525 + 1013904223;
      34             : }
      35             : 
      36           1 : static void test_av_parse_video_rate(void)
      37             : {
      38             :     int i;
      39             :     static const char *const rates[] = {
      40             :         "-inf",
      41             :         "inf",
      42             :         "nan",
      43             :         "123/0",
      44             :         "-123 / 0",
      45             :         "",
      46             :         "/",
      47             :         " 123  /  321",
      48             :         "foo/foo",
      49             :         "foo/1",
      50             :         "1/foo",
      51             :         "0/0",
      52             :         "/0",
      53             :         "1/",
      54             :         "1",
      55             :         "0",
      56             :         "-123/123",
      57             :         "-foo",
      58             :         "123.23",
      59             :         ".23",
      60             :         "-.23",
      61             :         "-0.234",
      62             :         "-0.0000001",
      63             :         "  21332.2324   ",
      64             :         " -21332.2324   ",
      65             :     };
      66             : 
      67          26 :     for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) {
      68             :         int ret;
      69          25 :         AVRational q = { 0, 0 };
      70          25 :         ret = av_parse_video_rate(&q, rates[i]);
      71          25 :         printf("'%s' -> %d/%d %s\n",
      72             :                rates[i], q.num, q.den, ret ? "ERROR" : "OK");
      73             :     }
      74           1 : }
      75             : 
      76           1 : static void test_av_parse_color(void)
      77             : {
      78             :     int i;
      79             :     uint8_t rgba[4];
      80             :     static const char *const color_names[] = {
      81             :         "bikeshed",
      82             :         "RaNdOm",
      83             :         "foo",
      84             :         "red",
      85             :         "Red ",
      86             :         "RED",
      87             :         "Violet",
      88             :         "Yellow",
      89             :         "Red",
      90             :         "0x000000",
      91             :         "0x0000000",
      92             :         "0xff000000",
      93             :         "0x3e34ff",
      94             :         "0x3e34ffaa",
      95             :         "0xffXXee",
      96             :         "0xfoobar",
      97             :         "0xffffeeeeeeee",
      98             :         "#ff0000",
      99             :         "#ffXX00",
     100             :         "ff0000",
     101             :         "ffXX00",
     102             :         "red@foo",
     103             :         "random@10",
     104             :         "0xff0000@1.0",
     105             :         "red@",
     106             :         "red@0xfff",
     107             :         "red@0xf",
     108             :         "red@2",
     109             :         "red@0.1",
     110             :         "red@-1",
     111             :         "red@0.5",
     112             :         "red@1.0",
     113             :         "red@256",
     114             :         "red@10foo",
     115             :         "red@-1.0",
     116             :         "red@-0.0",
     117             :     };
     118             : 
     119           1 :     av_log_set_level(AV_LOG_DEBUG);
     120             : 
     121          37 :     for (i = 0;  i < FF_ARRAY_ELEMS(color_names); i++) {
     122          36 :         if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0)
     123          76 :             printf("%s -> R(%d) G(%d) B(%d) A(%d)\n",
     124          76 :                    color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
     125             :         else
     126          17 :             printf("%s -> error\n", color_names[i]);
     127             :     }
     128           1 : }
     129             : 
     130           1 : static void test_av_small_strptime(void)
     131             : {
     132             :     int i;
     133           1 :     struct tm tm = { 0 };
     134             :     struct fmt_timespec_entry {
     135             :         const char *fmt, *timespec;
     136           1 :     } fmt_timespec_entries[] = {
     137             :         { "%Y-%m-%d",                    "2012-12-21" },
     138             :         { "%Y - %m - %d",                "2012-12-21" },
     139             :         { "%Y-%m-%d %H:%M:%S",           "2012-12-21 20:12:21" },
     140             :         { "  %Y - %m - %d %H : %M : %S", "   2012 - 12 -  21   20 : 12 : 21" },
     141             :     };
     142             : 
     143           1 :     av_log_set_level(AV_LOG_DEBUG);
     144           5 :     for (i = 0;  i < FF_ARRAY_ELEMS(fmt_timespec_entries); i++) {
     145             :         char *p;
     146           4 :         struct fmt_timespec_entry *e = &fmt_timespec_entries[i];
     147           4 :         printf("fmt:'%s' spec:'%s' -> ", e->fmt, e->timespec);
     148           4 :         p = av_small_strptime(e->timespec, e->fmt, &tm);
     149           4 :         if (p) {
     150          12 :             printf("%04d-%02d-%2d %02d:%02d:%02d\n",
     151           8 :                    1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday,
     152             :                    tm.tm_hour, tm.tm_min, tm.tm_sec);
     153             :         } else {
     154           0 :             printf("error\n");
     155             :         }
     156             :     }
     157           1 : }
     158             : 
     159           1 : static void test_av_parse_time(void)
     160             : {
     161             :     int i;
     162             :     int64_t tv;
     163             :     time_t tvi;
     164             :     struct tm *tm;
     165             :     static char tzstr[] = "TZ=CET-1";
     166             :     static const char * const time_string[] = {
     167             :         "now",
     168             :         "12:35:46",
     169             :         "2000-12-20 0:02:47.5z",
     170             :         "2012 - 02-22  17:44:07",
     171             :         "2000-12-20T010247.6",
     172             :         "2000-12-12 1:35:46+05:30",
     173             :         "2002-12-12 22:30:40-02",
     174             :     };
     175             :     static const char * const duration_string[] = {
     176             :         "2:34:56.79",
     177             :         "-1:23:45.67",
     178             :         "42.1729",
     179             :         "-1729.42",
     180             :         "12:34",
     181             :     };
     182             : 
     183           1 :     av_log_set_level(AV_LOG_DEBUG);
     184           1 :     putenv(tzstr);
     185           1 :     printf("(now is 2012-03-17 09:14:13.2 +0100, local time is UTC+1)\n");
     186           8 :     for (i = 0;  i < FF_ARRAY_ELEMS(time_string); i++) {
     187           7 :         printf("%-24s -> ", time_string[i]);
     188           7 :         if (av_parse_time(&tv, time_string[i], 0)) {
     189           0 :             printf("error\n");
     190             :         } else {
     191           7 :             tvi = tv / 1000000;
     192           7 :             tm = gmtime(&tvi);
     193          28 :             printf("%14"PRIi64".%06d = %04d-%02d-%02dT%02d:%02d:%02dZ\n",
     194           7 :                    tv / 1000000, (int)(tv % 1000000),
     195          14 :                    tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
     196             :                    tm->tm_hour, tm->tm_min, tm->tm_sec);
     197             :         }
     198             :     }
     199           6 :     for (i = 0;  i < FF_ARRAY_ELEMS(duration_string); i++) {
     200           5 :         printf("%-24s -> ", duration_string[i]);
     201           5 :         if (av_parse_time(&tv, duration_string[i], 1)) {
     202           0 :             printf("error\n");
     203             :         } else {
     204           5 :             printf("%+21"PRIi64"\n", tv);
     205             :         }
     206             :     }
     207           1 : }
     208             : 
     209           1 : static void test_av_get_known_color_name(void)
     210             : {
     211             :     int i;
     212             :     const uint8_t *rgba;
     213             :     const char *color;
     214             : 
     215         141 :     for (i = 0; i < FF_ARRAY_ELEMS(color_table); ++i) {
     216         140 :         color = av_get_known_color_name(i, &rgba);
     217         140 :         if (color)
     218         560 :             printf("%s -> R(%d) G(%d) B(%d) A(%d)\n",
     219         560 :                     color, rgba[0], rgba[1], rgba[2], rgba[3]);
     220             :         else
     221           0 :             printf("Color ID: %d not found\n", i);
     222             :     }
     223           1 : }
     224             : 
     225           1 : static void test_av_find_info_tag(void)
     226             : {
     227             :     static const char args[] = "?tag1=val1&tag2=val2&tag3=val3&tag41=value 41&tag42=random1";
     228             :     static const char *tags[] = {"tag1", "tag2", "tag3", "tag4", "tag41", "41", "random1"};
     229             :     char buff[16];
     230             :     int i;
     231             : 
     232           8 :     for (i = 0; i < FF_ARRAY_ELEMS(tags); ++i) {
     233           7 :         if (av_find_info_tag(buff, sizeof(buff), tags[i], args))
     234           4 :             printf("%d. %s found: %s\n", i, tags[i], buff);
     235             :         else
     236           3 :             printf("%d. %s not found\n", i, tags[i]);
     237             :     }
     238           1 : }
     239             : 
     240           1 : int main(void)
     241             : {
     242           1 :     printf("Testing av_parse_video_rate()\n");
     243           1 :     test_av_parse_video_rate();
     244             : 
     245           1 :     printf("\nTesting av_parse_color()\n");
     246           1 :     test_av_parse_color();
     247             : 
     248           1 :     printf("\nTesting av_small_strptime()\n");
     249           1 :     test_av_small_strptime();
     250             : 
     251           1 :     printf("\nTesting av_parse_time()\n");
     252           1 :     test_av_parse_time();
     253             : 
     254           1 :     printf("\nTesting av_get_known_color_name()\n");
     255           1 :     test_av_get_known_color_name();
     256             : 
     257           1 :     printf("\nTesting av_find_info_tag()\n");
     258           1 :     test_av_find_info_tag();
     259           1 :     return 0;
     260             : }

Generated by: LCOV version 1.12