summaryrefslogtreecommitdiff
path: root/libc/test/src/stdio/scanf_core/reader_test.cpp
blob: 4cafc81251f0b8343db3f44558a443aa56e8c1f0 (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
//===-- Unittests for the scanf String Reader -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/string_view.h"
#include "src/stdio/scanf_core/string_reader.h"

#include "test/UnitTest/Test.h"

TEST(LlvmLibcScanfStringReaderTest, Constructor) {
  char str[10];
  // buff_len justneeds to be a big number. The specific value isn't important
  // in the real world.
  LIBC_NAMESPACE::scanf_core::StringReader reader(const_cast<char *>(str),
                                                  1000000);
}

TEST(LlvmLibcScanfStringReaderTest, SimpleRead) {
  const char *str = "abc";
  LIBC_NAMESPACE::scanf_core::StringReader reader(const_cast<char *>(str),
                                                  1000000);

  for (size_t i = 0; i < sizeof("abc"); ++i) {
    ASSERT_EQ(str[i], reader.getc());
  }
}

TEST(LlvmLibcScanfStringReaderTest, ReadAndReverse) {
  const char *str = "abcDEF123";
  LIBC_NAMESPACE::scanf_core::StringReader reader(const_cast<char *>(str),
                                                  1000000);

  for (size_t i = 0; i < 5; ++i) {
    ASSERT_EQ(str[i], reader.getc());
  }

  // Move back by 3, cursor should now be on 2
  reader.ungetc(str[4]);
  reader.ungetc(str[3]);
  reader.ungetc(str[2]);

  for (size_t i = 2; i < 7; ++i) {
    ASSERT_EQ(str[i], reader.getc());
  }

  // Move back by 2, cursor should now be on 5
  reader.ungetc(str[6]);
  reader.ungetc(str[5]);

  for (size_t i = 5; i < 10; ++i) {
    ASSERT_EQ(str[i], reader.getc());
  }

  // Move back by 10, which should be back to the start.
  for (size_t i = 0; i < 10; ++i) {
    reader.ungetc(str[9 - i]);
  }

  // Check the whole string.
  for (size_t i = 0; i < sizeof("abcDEF123"); ++i) {
    ASSERT_EQ(str[i], reader.getc());
  }
}