aboutsummaryrefslogtreecommitdiff
path: root/stdio-common/tst-scanf-nan.c
blob: 7450b37c4e61260883abf6b459beb6c049141158 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Test scanf formats for nan, nan(), nan(n-char-sequence) types.
   Copyright The GNU Toolchain Authors.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <stdint.h>
#include <stdio.h>

#include <support/check.h>

#define CHECK_SCANF_RET(OK, STR, FMT, ...)                                    \
  do                                                                          \
    {                                                                         \
      int ret = sscanf (STR, FMT, __VA_ARGS__);                               \
      TEST_VERIFY (ret == (OK));                                              \
    }                                                                         \
  while (0)

/* Valid nan types:
   1. nan
   2. nan()
   3. nan([a-zA-Z0-9_]+)
   Any other nan format is invalid and should produce a conversion error.
   The return value denotes the number of valid conversions.  On conversion
   error the rest of the input is discarded.  */
static int
do_test (void)
{
  int a;
  float b;
  double c;
  long double d;

  /* All valid inputs.  */
  CHECK_SCANF_RET (1, "nan", "%lf", &c);
  CHECK_SCANF_RET (1, "nan()", "%lf", &c);
  CHECK_SCANF_RET (1, "nan(12345)", "%lf", &c);
  CHECK_SCANF_RET (2, "nan12", "%lf%d", &c, &a);
  CHECK_SCANF_RET (2, "nan nan()", "%f%Lf", &b, &d);
  CHECK_SCANF_RET (2, "nan nan(12345foo)", "%lf%Lf", &c, &d);
  CHECK_SCANF_RET (3, "nan nan() 12.234", "%lf%Lf%f", &c, &d, &b);
  CHECK_SCANF_RET (4, "nannan()nan(foo)1234", "%lf%f%Lf%d", &c, &b, &d, &a);

  /* Partially valid inputs.  */
  CHECK_SCANF_RET (1, "nan( )", "%3lf", &c);
  CHECK_SCANF_RET (1, "nan nan(", "%lf%f", &c, &b);

  /* Invalid inputs.  */

  /* Dangling parentheses.  */
  CHECK_SCANF_RET (0, "nan(", "%lf", &c);
  CHECK_SCANF_RET (0, "nan(123", "%lf", &c);
  CHECK_SCANF_RET (0, "nan(12345", "%lf%d", &c, &a);

  /* Field width is not sufficient for valid conversion.  */
  CHECK_SCANF_RET (0, "nan()", "%4Lf", &d);
  CHECK_SCANF_RET (0, "nan(1", "%5lf", &c);

  /* Space is not a valid character.  */
  CHECK_SCANF_RET (0, "nan( )", "%lf", &c);
  CHECK_SCANF_RET (0, "nan( )12.34", "%Lf%f", &d, &b);
  CHECK_SCANF_RET (0, "nan(12 foo)", "%f", &b);

  /* Period '.' is not a valid character.  */
  CHECK_SCANF_RET (0, "nan(12.34) nan(FooBar)", "%lf%Lf", &c, &d);

  return 0;
}

#include <support/test-driver.c>