mirror of
https://github.com/red0124/ssp.git
synced 2025-12-16 14:49:56 +01:00
Compare commits
69 Commits
v1.6.0
...
e89e268280
| Author | SHA1 | Date | |
|---|---|---|---|
| e89e268280 | |||
| aacf690640 | |||
| f8c5757d99 | |||
| ce03c371ae | |||
| 6c859959d6 | |||
| 4434db29f6 | |||
|
|
fdd153b63e | ||
| c05ab35a33 | |||
| 8d1637cad1 | |||
| 7530be1c44 | |||
| 7062888d72 | |||
| f04ede3a49 | |||
| 4bedc32b63 | |||
| 7822351a0b | |||
| a755f6c455 | |||
|
|
4db88c0490 | ||
| 8b72deb1ed | |||
| 672b89b213 | |||
| b9d2c2aad9 | |||
| 32cbfe1d17 | |||
| 848689451c | |||
| b7e5dd28b8 | |||
| e316558a7b | |||
| 1c6eacad30 | |||
| 5ac506d8f0 | |||
| 6e27f35209 | |||
| 49a3a20e68 | |||
| 1d0911ab3c | |||
| 7d3d02f11d | |||
| 17c21e260f | |||
| 2504c3574d | |||
| 0bd78120f3 | |||
| ed8f4e3147 | |||
| d4c9227830 | |||
| f232c7d995 | |||
| 14ccb88664 | |||
|
|
34833837ba | ||
| 5f27595ec1 | |||
| 65371d05f3 | |||
| 31b924736f | |||
| ffca94d47d | |||
| 7ba66ff99d | |||
| f1e127dd2b | |||
| 3e3eb1b61c | |||
| b9d8eb860e | |||
| 4139b50cd9 | |||
| 8924ad12e5 | |||
| 254bd24bbd | |||
| 55d1bbcf86 | |||
| 3e3c922624 | |||
| c0e6e56364 | |||
| 66f57ba66a | |||
| d37ec12bb5 | |||
| 9d7441b178 | |||
| dcf7e924ad | |||
| 41c4bf9d35 | |||
| 7b16f24d03 | |||
| 535138d9b8 | |||
| f106889ada | |||
| e7045ce437 | |||
| d86c8e9fe8 | |||
| c0ef691889 | |||
| 80c189f9c5 | |||
| 07373ea043 | |||
| 236b5da9c2 | |||
| a1f01ec5cb | |||
| cd6c2df359 | |||
| 9afe24785b | |||
| 479cf4bbe7 |
71
.github/workflows/coverage.yml
vendored
Normal file
71
.github/workflows/coverage.yml
vendored
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
name: coverage-ci
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- feature/**
|
||||||
|
- improvement/**
|
||||||
|
- bugfix/**
|
||||||
|
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- feature/**
|
||||||
|
- improvement/**
|
||||||
|
- bugfix/**
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test_coverage:
|
||||||
|
if: >-
|
||||||
|
! contains(toJSON(github.event.commits.*.message), '[skip ci]') &&
|
||||||
|
! contains(toJSON(github.event.commits.*.message), '[skip github]')
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
name: "Coverage"
|
||||||
|
|
||||||
|
container:
|
||||||
|
image: gcc:latest
|
||||||
|
options: -v /usr/local:/host_usr_local
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v1
|
||||||
|
|
||||||
|
- uses: friendlyanon/fetch-core-count@v1
|
||||||
|
id: cores
|
||||||
|
|
||||||
|
- name: CMake
|
||||||
|
run: echo "/host_usr_local/bin" >> $GITHUB_PATH
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: script/ci_install_deps.sh
|
||||||
|
|
||||||
|
- name: Install test coverage tools
|
||||||
|
run: |
|
||||||
|
apt update
|
||||||
|
apt install -y gcovr lcov
|
||||||
|
|
||||||
|
- name: Configure
|
||||||
|
run: cmake -S test -B build -D CMAKE_BUILD_TYPE=Debug -D CMAKE_CXX_FLAGS="-Wall -fprofile-arcs -ftest-coverage --coverage"
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: cmake --build build -j ${{steps.cores.outputs.count}}
|
||||||
|
|
||||||
|
- name: Run
|
||||||
|
working-directory: build
|
||||||
|
run: ctest --output-on-failure -j ${{steps.cores.outputs.count}}
|
||||||
|
|
||||||
|
- name: Generate coverage report
|
||||||
|
run: |
|
||||||
|
lcov -d . -c -o out.info --rc lcov_branch_coverage=1 --no-external
|
||||||
|
lcov -e out.info '*include/ss*hpp' -o filtered.info
|
||||||
|
|
||||||
|
- name: Invoke coveralls
|
||||||
|
uses: coverallsapp/github-action@v2
|
||||||
|
with:
|
||||||
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
||||||
|
file: filtered.info
|
||||||
|
format: lcov
|
||||||
51
.github/workflows/single-header.yml
vendored
Normal file
51
.github/workflows/single-header.yml
vendored
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
name: single-header-ci
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- feature/**
|
||||||
|
- improvement/**
|
||||||
|
- bugfix/**
|
||||||
|
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- feature/**
|
||||||
|
- improvement/**
|
||||||
|
- bugfix/**
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
single_header_tests:
|
||||||
|
if: >-
|
||||||
|
! contains(toJSON(github.event.commits.*.message), '[skip ci]') &&
|
||||||
|
! contains(toJSON(github.event.commits.*.message), '[skip github]')
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
name: "Single Header Test"
|
||||||
|
|
||||||
|
container:
|
||||||
|
image: gcc:latest
|
||||||
|
options: -v /usr/local:/host_usr_local
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v1
|
||||||
|
|
||||||
|
- uses: friendlyanon/fetch-core-count@v1
|
||||||
|
id: cores
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
apt-get update
|
||||||
|
apt-get install -y git
|
||||||
|
|
||||||
|
- name: Single header update check
|
||||||
|
run: |
|
||||||
|
script/single_header_generator.py > tmp.hpp
|
||||||
|
diff ssp.hpp tmp.hpp
|
||||||
|
|
||||||
|
- name: Single header compile check
|
||||||
|
run: ./test/test_single_header.sh
|
||||||
18
.github/workflows/ubuntu-latest-clang.yml
vendored
18
.github/workflows/ubuntu-latest-clang.yml
vendored
@@ -1,6 +1,8 @@
|
|||||||
name: ubuntu-latest-clang-ci
|
name: ubuntu-latest-clang-ci
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
@@ -23,20 +25,22 @@ jobs:
|
|||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
version: [11, 10, 9, 8, 7]
|
# cmake clang12 is not able to compile a simple test program.
|
||||||
|
# /usr/bin/ld: cannot find -lunwind
|
||||||
|
version: ['latest', '15', '14', '13', '11', '10', '9', '8', '7']
|
||||||
|
type: [Release, Debug]
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
name: Clang ${{ matrix.version }}
|
name: "Clang ${{matrix.version}}: ${{matrix.type}}"
|
||||||
|
|
||||||
container:
|
container:
|
||||||
image: teeks99/clang-ubuntu:${{ matrix.version }}
|
image: silkeh/clang:${{matrix.version}}
|
||||||
|
|
||||||
options: -v /usr/local:/host_usr_local
|
options: -v /usr/local:/host_usr_local
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CC: clang-${{ matrix.version }}
|
CC: clang
|
||||||
CXX: clang++-${{ matrix.version }}
|
CXX: clang++
|
||||||
CXXFLAGS: -stdlib=libc++
|
CXXFLAGS: -stdlib=libc++
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
@@ -55,7 +59,7 @@ jobs:
|
|||||||
script/ci_install_deps.sh
|
script/ci_install_deps.sh
|
||||||
|
|
||||||
- name: Configure
|
- name: Configure
|
||||||
run: cmake -S test -B build -D CMAKE_BUILD_TYPE=Debug
|
run: cmake -S test -B build -DCMAKE_BUILD_TYPE=${{matrix.type}}
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: cmake --build build -j ${{steps.cores.outputs.count}}
|
run: cmake --build build -j ${{steps.cores.outputs.count}}
|
||||||
|
|||||||
10
.github/workflows/ubuntu-latest-gcc.yml
vendored
10
.github/workflows/ubuntu-latest-gcc.yml
vendored
@@ -1,6 +1,8 @@
|
|||||||
name: ubuntu-latest-gcc-ci
|
name: ubuntu-latest-gcc-ci
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
@@ -23,15 +25,15 @@ jobs:
|
|||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
version: [10, 9, 8]
|
version: ['latest', '12', '11', '10', '9', '8']
|
||||||
|
type: [Release, Debug]
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
name: GCC ${{ matrix.version }}
|
name: "GCC ${{matrix.version}}: ${{matrix.type}}"
|
||||||
|
|
||||||
container:
|
container:
|
||||||
image: gcc:${{matrix.version}}
|
image: gcc:${{matrix.version}}
|
||||||
|
|
||||||
options: -v /usr/local:/host_usr_local
|
options: -v /usr/local:/host_usr_local
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
@@ -47,7 +49,7 @@ jobs:
|
|||||||
run: script/ci_install_deps.sh
|
run: script/ci_install_deps.sh
|
||||||
|
|
||||||
- name: Configure
|
- name: Configure
|
||||||
run: cmake -S test -B build -D CMAKE_BUILD_TYPE=Debug
|
run: cmake -S test -B build -D CMAKE_BUILD_TYPE=${{matrix.type}}
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: cmake --build build -j ${{steps.cores.outputs.count}}
|
run: cmake --build build -j ${{steps.cores.outputs.count}}
|
||||||
|
|||||||
6
.github/workflows/ubuntu-latest-icc.yml
vendored
6
.github/workflows/ubuntu-latest-icc.yml
vendored
@@ -1,6 +1,8 @@
|
|||||||
name: ubuntu-latest-icc-ci
|
name: ubuntu-latest-icc-ci
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
@@ -27,6 +29,7 @@ jobs:
|
|||||||
! contains(toJSON(github.event.commits.*.message), '[skip github]')
|
! contains(toJSON(github.event.commits.*.message), '[skip github]')
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
shell: bash
|
shell: bash
|
||||||
@@ -50,7 +53,8 @@ jobs:
|
|||||||
compiler-${{hashFiles('**/scripts/cache_exclude_linux.sh')}}
|
compiler-${{hashFiles('**/scripts/cache_exclude_linux.sh')}}
|
||||||
|
|
||||||
- name: Install icc
|
- name: Install icc
|
||||||
run: script/ci_install_icc.sh $LINUX_HPCKIT_URL $LINUX_CPP_COMPONENTS_WEB
|
run: >-
|
||||||
|
script/ci_install_icc.sh $LINUX_HPCKIT_URL $LINUX_CPP_COMPONENTS_WEB
|
||||||
|
|
||||||
- name: CMake
|
- name: CMake
|
||||||
run: echo "/host_usr_local/bin" >> $GITHUB_PATH
|
run: echo "/host_usr_local/bin" >> $GITHUB_PATH
|
||||||
|
|||||||
6
.github/workflows/win-msvc.yml
vendored
6
.github/workflows/win-msvc.yml
vendored
@@ -1,6 +1,8 @@
|
|||||||
name: win-msvc-ci
|
name: win-msvc-ci
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
@@ -25,8 +27,6 @@ jobs:
|
|||||||
run:
|
run:
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
runs-on: ${{ matrix.config.os }}
|
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
@@ -40,6 +40,8 @@ jobs:
|
|||||||
build: [Debug, Release]
|
build: [Debug, Release]
|
||||||
platform: [Win32, x64]
|
platform: [Win32, x64]
|
||||||
|
|
||||||
|
runs-on: ${{matrix.config.os}}
|
||||||
|
|
||||||
name: "${{matrix.config.vs}}: ${{matrix.platform}}: ${{matrix.build}}"
|
name: "${{matrix.config.vs}}: ${{matrix.platform}}: ${{matrix.build}}"
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
|||||||
35
.github/workflows/win-msys2-clang.yml
vendored
35
.github/workflows/win-msys2-clang.yml
vendored
@@ -1,6 +1,8 @@
|
|||||||
name: win-msys2-clang-ci
|
name: win-msys2-clang-ci
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
@@ -21,45 +23,42 @@ jobs:
|
|||||||
! contains(toJSON(github.event.commits.*.message), '[skip ci]') &&
|
! contains(toJSON(github.event.commits.*.message), '[skip ci]') &&
|
||||||
! contains(toJSON(github.event.commits.*.message), '[skip github]')
|
! contains(toJSON(github.event.commits.*.message), '[skip github]')
|
||||||
|
|
||||||
name: ${{ matrix.msystem }}
|
|
||||||
runs-on: windows-latest
|
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
shell: msys2 {0}
|
shell: msys2 {0}
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
os: [windows-2019, windows-latest]
|
||||||
|
type: [Release, Debug]
|
||||||
|
config:
|
||||||
- msystem: "MINGW64"
|
- msystem: "MINGW64"
|
||||||
install: >-
|
install: >-
|
||||||
git mingw-w64-x86_64-cmake mingw-w64-x86_64-ninja
|
git mingw-w64-x86_64-cmake mingw-w64-x86_64-ninja
|
||||||
mingw-w64-x86_64-clang
|
mingw-w64-x86_64-clang
|
||||||
type: Release
|
|
||||||
- msystem: "MINGW32"
|
- msystem: "MINGW32"
|
||||||
install: >-
|
install: >-
|
||||||
git mingw-w64-i686-cmake mingw-w64-i686-ninja
|
git mingw-w64-i686-cmake mingw-w64-i686-ninja
|
||||||
mingw-w64-i686-clang
|
mingw-w64-i686-clang
|
||||||
type: Release
|
|
||||||
- msystem: "MINGW64"
|
runs-on: ${{matrix.os}}
|
||||||
install: >-
|
|
||||||
git mingw-w64-x86_64-cmake mingw-w64-x86_64-ninja
|
name: "${{matrix.config.msystem}}: ${{matrix.os}}: ${{matrix.type}}"
|
||||||
mingw-w64-x86_64-clang
|
|
||||||
type: Debug
|
|
||||||
- msystem: "MINGW32"
|
|
||||||
install: >-
|
|
||||||
git mingw-w64-i686-cmake mingw-w64-i686-ninja
|
|
||||||
mingw-w64-i686-clang
|
|
||||||
type: Debug
|
|
||||||
env:
|
env:
|
||||||
CMAKE_GENERATOR: Ninja
|
CMAKE_GENERATOR: Ninja
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
- uses: msys2/setup-msys2@v2
|
- uses: msys2/setup-msys2@v2
|
||||||
with:
|
with:
|
||||||
update: true
|
update: true
|
||||||
msystem: ${{ matrix.msystem }}
|
msystem: ${{matrix.config.msystem}}
|
||||||
install: ${{ matrix.install }}
|
install: ${{matrix.config.install}}
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: script/ci_install_deps.sh
|
run: script/ci_install_deps.sh
|
||||||
@@ -67,7 +66,7 @@ jobs:
|
|||||||
- name: Configure
|
- name: Configure
|
||||||
run: >-
|
run: >-
|
||||||
cmake -DCMAKE_CXX_COMPILER=clang++ -S test -B build
|
cmake -DCMAKE_CXX_COMPILER=clang++ -S test -B build
|
||||||
-DCMAKE_BUILD_TYPE=Debug
|
-DCMAKE_BUILD_TYPE=${{matrix.type}}
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: cmake --build build -j ${{steps.cores.outputs.count}}
|
run: cmake --build build -j ${{steps.cores.outputs.count}}
|
||||||
|
|||||||
35
.github/workflows/win-msys2-gcc.yml
vendored
35
.github/workflows/win-msys2-gcc.yml
vendored
@@ -1,6 +1,8 @@
|
|||||||
name: win-msys2-gcc-ci
|
name: win-msys2-gcc-ci
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
@@ -21,51 +23,48 @@ jobs:
|
|||||||
! contains(toJSON(github.event.commits.*.message), '[skip ci]') &&
|
! contains(toJSON(github.event.commits.*.message), '[skip ci]') &&
|
||||||
! contains(toJSON(github.event.commits.*.message), '[skip github]')
|
! contains(toJSON(github.event.commits.*.message), '[skip github]')
|
||||||
|
|
||||||
name: ${{ matrix.msystem }}
|
|
||||||
runs-on: windows-latest
|
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
shell: msys2 {0}
|
shell: msys2 {0}
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
os: [windows-2019, windows-latest]
|
||||||
|
type: [Release, Debug]
|
||||||
|
config:
|
||||||
- msystem: "MINGW64"
|
- msystem: "MINGW64"
|
||||||
install: >-
|
install: >-
|
||||||
git mingw-w64-x86_64-cmake mingw-w64-x86_64-ninja
|
git mingw-w64-x86_64-cmake mingw-w64-x86_64-ninja
|
||||||
mingw-w64-x86_64-gcc
|
mingw-w64-x86_64-gcc
|
||||||
type: Release
|
|
||||||
- msystem: "MINGW32"
|
- msystem: "MINGW32"
|
||||||
install: >-
|
install: >-
|
||||||
git mingw-w64-i686-cmake mingw-w64-i686-ninja
|
git mingw-w64-i686-cmake mingw-w64-i686-ninja
|
||||||
mingw-w64-i686-gcc
|
mingw-w64-i686-gcc
|
||||||
type: Release
|
|
||||||
- msystem: "MINGW64"
|
runs-on: ${{matrix.os}}
|
||||||
install: >-
|
|
||||||
git mingw-w64-x86_64-cmake mingw-w64-x86_64-ninja
|
name: "${{matrix.config.msystem}}: ${{matrix.os}}: ${{matrix.type}}"
|
||||||
mingw-w64-x86_64-gcc
|
|
||||||
type: Debug
|
|
||||||
- msystem: "MINGW32"
|
|
||||||
install: >-
|
|
||||||
git mingw-w64-i686-cmake mingw-w64-i686-ninja
|
|
||||||
mingw-w64-i686-gcc
|
|
||||||
type: Debug
|
|
||||||
env:
|
env:
|
||||||
CMAKE_GENERATOR: Ninja
|
CMAKE_GENERATOR: Ninja
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
- uses: msys2/setup-msys2@v2
|
- uses: msys2/setup-msys2@v2
|
||||||
with:
|
with:
|
||||||
update: true
|
update: true
|
||||||
msystem: ${{ matrix.msystem }}
|
msystem: ${{matrix.config.msystem}}
|
||||||
install: ${{ matrix.install }}
|
install: ${{matrix.config.install}}
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: script/ci_install_deps.sh
|
run: script/ci_install_deps.sh
|
||||||
|
|
||||||
- name: Configure
|
- name: Configure
|
||||||
run: cmake -S test -B build -D CMAKE_BUILD_TYPE=Debug
|
run: cmake -S test -B build -D CMAKE_BUILD_TYPE=${{matrix.type}}
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: cmake --build build -j ${{steps.cores.outputs.count}}
|
run: cmake --build build -j ${{steps.cores.outputs.count}}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.14)
|
|||||||
|
|
||||||
project(
|
project(
|
||||||
ssp
|
ssp
|
||||||
VERSION 1.5.0
|
VERSION 1.6.2
|
||||||
DESCRIPTION "csv parser"
|
DESCRIPTION "csv parser"
|
||||||
HOMEPAGE_URL "https://github.com/red0124/ssp"
|
HOMEPAGE_URL "https://github.com/red0124/ssp"
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
|
|||||||
@@ -8,12 +8,14 @@
|
|||||||
```
|
```
|
||||||
|
|
||||||
[](https://opensource.org/licenses/MIT)
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
[](https://coveralls.io/github/red0124/ssp?branch=master)
|
||||||
|
[](https://github.com/red0124/ssp/actions/workflows/single-header.yml)
|
||||||
[](https://github.com/red0124/ssp/actions/workflows/ubuntu-latest-gcc.yml)
|
[](https://github.com/red0124/ssp/actions/workflows/ubuntu-latest-gcc.yml)
|
||||||
[](https://github.com/red0124/ssp/actions/workflows/ubuntu-latest-clang.yml)
|
[](https://github.com/red0124/ssp/actions/workflows/ubuntu-latest-clang.yml)
|
||||||
[](https://github.com/red0124/ssp/actions/workflows/ubuntu-latest-icc.yml)
|
[](https://github.com/red0124/ssp/actions/workflows/ubuntu-latest-icc.yml)
|
||||||
[](https://github.com/red0124/ssp/actions/workflows/win-msys2-gcc.yml)
|
[](https://github.com/red0124/ssp/actions/workflows/win-msys2-gcc.yml)
|
||||||
[](https://github.com/red0124/ssp/actions/workflows/win-msys2-clang.yml)
|
[](https://github.com/red0124/ssp/actions/workflows/win-msys2-clang.yml)
|
||||||
[](https://github.com/red0124/ssp/actions/workflows/win-msvc.yml)
|
[](https://github.com/red0124/ssp/actions/workflows/win-msvc.yml)
|
||||||
|
|
||||||
A header only "csv" parser which is fast and versatile with modern C++ api. Requires compiler with C++17 support. [Can also be used to convert strings to specific types.](#the-converter)
|
A header only "csv" parser which is fast and versatile with modern C++ api. Requires compiler with C++17 support. [Can also be used to convert strings to specific types.](#the-converter)
|
||||||
|
|
||||||
|
|||||||
@@ -26,12 +26,12 @@ inline void assert_throw_on_error_not_defined() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if __unix__
|
#if __unix__
|
||||||
inline ssize_t get_line(char** lineptr, size_t* n, FILE* stream) {
|
inline ssize_t get_line_file(char** lineptr, size_t* n, FILE* stream) {
|
||||||
return getline(lineptr, n, stream);
|
return getline(lineptr, n, stream);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
using ssize_t = int64_t;
|
using ssize_t = int64_t;
|
||||||
inline ssize_t get_line(char** lineptr, size_t* n, FILE* stream) {
|
inline ssize_t get_line_file(char** lineptr, size_t* n, FILE* stream) {
|
||||||
size_t pos;
|
size_t pos;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
|
|||||||
@@ -379,7 +379,6 @@ private:
|
|||||||
return extract_tuple<Ts...>(elems);
|
return extract_tuple<Ts...>(elems);
|
||||||
}
|
}
|
||||||
|
|
||||||
// do not know how to specialize by return type :(
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
no_void_validator_tup_t<std::tuple<Ts...>> convert_impl(
|
no_void_validator_tup_t<std::tuple<Ts...>> convert_impl(
|
||||||
const split_data& elems, const std::tuple<Ts...>*) {
|
const split_data& elems, const std::tuple<Ts...>*) {
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "type_traits.hpp"
|
#include "type_traits.hpp"
|
||||||
|
#include <charconv>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <charconv>
|
|
||||||
|
|
||||||
#ifndef SSP_DISABLE_FAST_FLOAT
|
#ifndef SSP_DISABLE_FAST_FLOAT
|
||||||
#include <fast_float/fast_float.h>
|
#include <fast_float/fast_float.h>
|
||||||
@@ -16,7 +16,6 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// TODO try from_chars for integer conversions
|
|
||||||
namespace ss {
|
namespace ss {
|
||||||
|
|
||||||
////////////////
|
////////////////
|
||||||
@@ -42,16 +41,23 @@ std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
||||||
const char* const begin, const char* const end) {
|
const char* const begin, const char* const end) {
|
||||||
|
static_assert(!std::is_same_v<T, long double>,
|
||||||
|
"Conversion to long double is disabled");
|
||||||
|
|
||||||
constexpr static auto buff_max = 64;
|
constexpr static auto buff_max = 64;
|
||||||
char buff[buff_max];
|
char short_buff[buff_max];
|
||||||
size_t string_range = std::distance(begin, end);
|
size_t string_range = std::distance(begin, end);
|
||||||
|
std::string long_buff;
|
||||||
|
|
||||||
|
char* buff;
|
||||||
if (string_range > buff_max) {
|
if (string_range > buff_max) {
|
||||||
return std::nullopt;
|
long_buff = std::string{begin, end};
|
||||||
}
|
buff = long_buff.data();
|
||||||
|
} else {
|
||||||
std::copy_n(begin, string_range, buff);
|
buff = short_buff;
|
||||||
buff[string_range] = '\0';
|
buff[string_range] = '\0';
|
||||||
|
std::copy_n(begin, string_range, buff);
|
||||||
|
}
|
||||||
|
|
||||||
T ret;
|
T ret;
|
||||||
char* parse_end = nullptr;
|
char* parse_end = nullptr;
|
||||||
@@ -60,8 +66,6 @@ std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
|||||||
ret = std::strtof(buff, &parse_end);
|
ret = std::strtof(buff, &parse_end);
|
||||||
} else if constexpr (std::is_same_v<T, double>) {
|
} else if constexpr (std::is_same_v<T, double>) {
|
||||||
ret = std::strtod(buff, &parse_end);
|
ret = std::strtod(buff, &parse_end);
|
||||||
} else if constexpr (std::is_same_v<T, long double>) {
|
|
||||||
ret = std::strtold(buff, &parse_end);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parse_end != buff + string_range) {
|
if (parse_end != buff + string_range) {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// TODO add single header tests
|
|
||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
#include "converter.hpp"
|
#include "converter.hpp"
|
||||||
#include "exception.hpp"
|
#include "exception.hpp"
|
||||||
@@ -49,6 +48,18 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parser(const char* const csv_data_buffer, size_t csv_data_size,
|
||||||
|
const std::string& delim = ss::default_delimiter)
|
||||||
|
: file_name_{"buffer line"},
|
||||||
|
reader_{csv_data_buffer, csv_data_size, delim} {
|
||||||
|
read_line();
|
||||||
|
if constexpr (ignore_header) {
|
||||||
|
ignore_next();
|
||||||
|
} else {
|
||||||
|
raw_header_ = reader_.get_buffer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
parser(parser&& other) = default;
|
parser(parser&& other) = default;
|
||||||
parser& operator=(parser&& other) = default;
|
parser& operator=(parser&& other) = default;
|
||||||
|
|
||||||
@@ -156,7 +167,7 @@ public:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (header_.empty()) {
|
if (header_.empty() && !eof()) {
|
||||||
split_header_data();
|
split_header_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -642,18 +653,27 @@ private:
|
|||||||
: delim_{delim}, file_{fopen(file_name_.c_str(), "rb")} {
|
: delim_{delim}, file_{fopen(file_name_.c_str(), "rb")} {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reader(const char* const buffer, size_t csv_data_size,
|
||||||
|
const std::string& delim)
|
||||||
|
: delim_{delim}, csv_data_buffer_{buffer},
|
||||||
|
csv_data_size_{csv_data_size} {
|
||||||
|
}
|
||||||
|
|
||||||
reader(reader&& other)
|
reader(reader&& other)
|
||||||
: buffer_{other.buffer_},
|
: buffer_{other.buffer_},
|
||||||
next_line_buffer_{other.next_line_buffer_},
|
next_line_buffer_{other.next_line_buffer_},
|
||||||
helper_buffer_{other.helper_buffer_}, converter_{std::move(
|
helper_buffer_{other.helper_buffer_},
|
||||||
other.converter_)},
|
converter_{std::move(other.converter_)},
|
||||||
next_line_converter_{std::move(other.next_line_converter_)},
|
next_line_converter_{std::move(other.next_line_converter_)},
|
||||||
buffer_size_{other.buffer_size_},
|
buffer_size_{other.buffer_size_},
|
||||||
next_line_buffer_size_{other.next_line_buffer_size_},
|
next_line_buffer_size_{other.next_line_buffer_size_},
|
||||||
helper_size_{other.helper_size_}, delim_{std::move(other.delim_)},
|
helper_buffer_size{other.helper_buffer_size},
|
||||||
file_{other.file_}, crlf_{other.crlf_},
|
delim_{std::move(other.delim_)}, file_{other.file_},
|
||||||
line_number_{other.line_number_}, next_line_size_{
|
csv_data_buffer_{other.csv_data_buffer_},
|
||||||
other.next_line_size_} {
|
csv_data_size_{other.csv_data_size_},
|
||||||
|
curr_char_{other.curr_char_}, crlf_{other.crlf_},
|
||||||
|
line_number_{other.line_number_},
|
||||||
|
next_line_size_{other.next_line_size_} {
|
||||||
other.buffer_ = nullptr;
|
other.buffer_ = nullptr;
|
||||||
other.next_line_buffer_ = nullptr;
|
other.next_line_buffer_ = nullptr;
|
||||||
other.helper_buffer_ = nullptr;
|
other.helper_buffer_ = nullptr;
|
||||||
@@ -669,9 +689,12 @@ private:
|
|||||||
next_line_converter_ = std::move(other.next_line_converter_);
|
next_line_converter_ = std::move(other.next_line_converter_);
|
||||||
buffer_size_ = other.buffer_size_;
|
buffer_size_ = other.buffer_size_;
|
||||||
next_line_buffer_size_ = other.next_line_buffer_size_;
|
next_line_buffer_size_ = other.next_line_buffer_size_;
|
||||||
helper_size_ = other.helper_size_;
|
helper_buffer_size = other.helper_buffer_size;
|
||||||
delim_ = std::move(other.delim_);
|
delim_ = std::move(other.delim_);
|
||||||
file_ = other.file_;
|
file_ = other.file_;
|
||||||
|
csv_data_buffer_ = other.csv_data_buffer_;
|
||||||
|
csv_data_size_ = other.csv_data_size_;
|
||||||
|
curr_char_ = other.curr_char_;
|
||||||
crlf_ = other.crlf_;
|
crlf_ = other.crlf_;
|
||||||
line_number_ = other.line_number_;
|
line_number_ = other.line_number_;
|
||||||
next_line_size_ = other.next_line_size_;
|
next_line_size_ = other.next_line_size_;
|
||||||
@@ -699,6 +722,60 @@ private:
|
|||||||
reader(const reader& other) = delete;
|
reader(const reader& other) = delete;
|
||||||
reader& operator=(const reader& other) = delete;
|
reader& operator=(const reader& other) = delete;
|
||||||
|
|
||||||
|
ssize_t get_line_buffer(char** lineptr, size_t* n,
|
||||||
|
const char* const buffer, size_t csv_data_size,
|
||||||
|
size_t& curr_char) {
|
||||||
|
size_t pos;
|
||||||
|
int c;
|
||||||
|
|
||||||
|
// TODO remove check
|
||||||
|
if (lineptr == nullptr || buffer == nullptr || n == nullptr) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
c = buffer[curr_char++];
|
||||||
|
if (curr_char >= csv_data_size) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO maybe remove this too
|
||||||
|
if (*lineptr == nullptr) {
|
||||||
|
*lineptr = static_cast<char*>(malloc(128));
|
||||||
|
if (*lineptr == nullptr) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*n = 128;
|
||||||
|
}
|
||||||
|
|
||||||
|
pos = 0;
|
||||||
|
while (curr_char <= csv_data_size) {
|
||||||
|
if (pos + 1 >= *n) {
|
||||||
|
size_t new_size = *n + (*n >> 2);
|
||||||
|
// TODO maybe remove this too
|
||||||
|
if (new_size < 128) {
|
||||||
|
new_size = 128;
|
||||||
|
}
|
||||||
|
char* new_ptr = static_cast<char*>(
|
||||||
|
realloc(static_cast<void*>(*lineptr), new_size));
|
||||||
|
// TODO check for failed malloc in the callee
|
||||||
|
if (new_ptr == nullptr) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*n = new_size;
|
||||||
|
*lineptr = new_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
(*lineptr)[pos++] = c;
|
||||||
|
if (c == '\n') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
c = buffer[curr_char++];
|
||||||
|
}
|
||||||
|
|
||||||
|
(*lineptr)[pos] = '\0';
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
// read next line each time in order to set eof_
|
// read next line each time in order to set eof_
|
||||||
bool read_next() {
|
bool read_next() {
|
||||||
next_line_converter_.clear_error();
|
next_line_converter_.clear_error();
|
||||||
@@ -709,8 +786,16 @@ private:
|
|||||||
if (next_line_buffer_size_ > 0) {
|
if (next_line_buffer_size_ > 0) {
|
||||||
next_line_buffer_[0] = '\0';
|
next_line_buffer_[0] = '\0';
|
||||||
}
|
}
|
||||||
ssize = get_line(&next_line_buffer_, &next_line_buffer_size_,
|
|
||||||
file_);
|
if (file_) {
|
||||||
|
ssize = get_line_file(&next_line_buffer_,
|
||||||
|
&next_line_buffer_size_, file_);
|
||||||
|
} else {
|
||||||
|
ssize = get_line_buffer(&next_line_buffer_,
|
||||||
|
&next_line_buffer_size_,
|
||||||
|
csv_data_buffer_, csv_data_size_,
|
||||||
|
curr_char_);
|
||||||
|
}
|
||||||
|
|
||||||
if (ssize == -1) {
|
if (ssize == -1) {
|
||||||
return false;
|
return false;
|
||||||
@@ -822,6 +907,10 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t remove_eol(char*& buffer, size_t ssize) {
|
size_t remove_eol(char*& buffer, size_t ssize) {
|
||||||
|
if (buffer[ssize - 1] != '\n') {
|
||||||
|
return ssize;
|
||||||
|
}
|
||||||
|
|
||||||
size_t size = ssize - 1;
|
size_t size = ssize - 1;
|
||||||
if (ssize >= 2 && buffer[ssize - 2] == '\r') {
|
if (ssize >= 2 && buffer[ssize - 2] == '\r') {
|
||||||
crlf_ = true;
|
crlf_ = true;
|
||||||
@@ -852,8 +941,17 @@ private:
|
|||||||
bool append_next_line_to_buffer(char*& buffer, size_t& size) {
|
bool append_next_line_to_buffer(char*& buffer, size_t& size) {
|
||||||
undo_remove_eol(buffer, size);
|
undo_remove_eol(buffer, size);
|
||||||
|
|
||||||
ssize_t next_ssize =
|
ssize_t next_ssize;
|
||||||
get_line(&helper_buffer_, &helper_size_, file_);
|
if (file_) {
|
||||||
|
next_ssize =
|
||||||
|
get_line_file(&helper_buffer_, &helper_buffer_size, file_);
|
||||||
|
} else {
|
||||||
|
next_ssize =
|
||||||
|
get_line_buffer(&helper_buffer_, &helper_buffer_size,
|
||||||
|
csv_data_buffer_, csv_data_size_,
|
||||||
|
curr_char_);
|
||||||
|
}
|
||||||
|
|
||||||
if (next_ssize == -1) {
|
if (next_ssize == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -880,11 +978,15 @@ private:
|
|||||||
|
|
||||||
size_t buffer_size_{0};
|
size_t buffer_size_{0};
|
||||||
size_t next_line_buffer_size_{0};
|
size_t next_line_buffer_size_{0};
|
||||||
size_t helper_size_{0};
|
size_t helper_buffer_size{0};
|
||||||
|
|
||||||
std::string delim_;
|
std::string delim_;
|
||||||
FILE* file_{nullptr};
|
FILE* file_{nullptr};
|
||||||
|
|
||||||
|
const char* csv_data_buffer_{nullptr};
|
||||||
|
size_t csv_data_size_{0};
|
||||||
|
size_t curr_char_{0};
|
||||||
|
|
||||||
bool crlf_{false};
|
bool crlf_{false};
|
||||||
size_t line_number_{0};
|
size_t line_number_{0};
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ project(
|
|||||||
'cpp_std=c++17',
|
'cpp_std=c++17',
|
||||||
'buildtype=debugoptimized',
|
'buildtype=debugoptimized',
|
||||||
'wrap_mode=forcefallback'],
|
'wrap_mode=forcefallback'],
|
||||||
version: '1.5.0',
|
version: '1.6.2',
|
||||||
meson_version:'>=0.54.0')
|
meson_version:'>=0.54.0')
|
||||||
|
|
||||||
fast_float_dep = dependency('fast_float')
|
fast_float_dep = dependency('fast_float')
|
||||||
|
|||||||
217
ssp.hpp
217
ssp.hpp
@@ -1,12 +1,12 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <charconv>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <limits>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -1465,7 +1465,6 @@ public:
|
|||||||
#else
|
#else
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// TODO try from_chars for integer conversions
|
|
||||||
namespace ss {
|
namespace ss {
|
||||||
|
|
||||||
////////////////
|
////////////////
|
||||||
@@ -1491,16 +1490,23 @@ std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
||||||
const char* const begin, const char* const end) {
|
const char* const begin, const char* const end) {
|
||||||
|
static_assert(!std::is_same_v<T, long double>,
|
||||||
|
"Conversion to long double is disabled");
|
||||||
|
|
||||||
constexpr static auto buff_max = 64;
|
constexpr static auto buff_max = 64;
|
||||||
char buff[buff_max];
|
char short_buff[buff_max];
|
||||||
size_t string_range = std::distance(begin, end);
|
size_t string_range = std::distance(begin, end);
|
||||||
|
std::string long_buff;
|
||||||
|
|
||||||
|
char* buff;
|
||||||
if (string_range > buff_max) {
|
if (string_range > buff_max) {
|
||||||
return std::nullopt;
|
long_buff = std::string{begin, end};
|
||||||
}
|
buff = long_buff.data();
|
||||||
|
} else {
|
||||||
std::copy_n(begin, string_range, buff);
|
buff = short_buff;
|
||||||
buff[string_range] = '\0';
|
buff[string_range] = '\0';
|
||||||
|
std::copy_n(begin, string_range, buff);
|
||||||
|
}
|
||||||
|
|
||||||
T ret;
|
T ret;
|
||||||
char* parse_end = nullptr;
|
char* parse_end = nullptr;
|
||||||
@@ -1509,8 +1515,6 @@ std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
|||||||
ret = std::strtof(buff, &parse_end);
|
ret = std::strtof(buff, &parse_end);
|
||||||
} else if constexpr (std::is_same_v<T, double>) {
|
} else if constexpr (std::is_same_v<T, double>) {
|
||||||
ret = std::strtod(buff, &parse_end);
|
ret = std::strtod(buff, &parse_end);
|
||||||
} else if constexpr (std::is_same_v<T, long double>) {
|
|
||||||
ret = std::strtold(buff, &parse_end);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parse_end != buff + string_range) {
|
if (parse_end != buff + string_range) {
|
||||||
@@ -1522,195 +1526,16 @@ std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
inline std::optional<short> from_char(char c) {
|
|
||||||
if (c >= '0' && c <= '9') {
|
|
||||||
return c - '0';
|
|
||||||
}
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(__clang__) && defined(__MINGW32__) && !defined(__MINGW64__)
|
|
||||||
#define MINGW32_CLANG
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// mingw32 clang does not support some of the builtin functions
|
|
||||||
#if (defined(__clang__) || defined(__GNUC__) || defined(__GUNG__)) && \
|
|
||||||
!defined(MINGW32_CLANG)
|
|
||||||
////////////////
|
|
||||||
// mul overflow detection
|
|
||||||
////////////////
|
|
||||||
template <typename T>
|
|
||||||
bool mul_overflow(T& result, T operand) {
|
|
||||||
return __builtin_mul_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool mul_overflow(int& result, int operand) {
|
|
||||||
return __builtin_smul_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool mul_overflow(long& result, long operand) {
|
|
||||||
return __builtin_smull_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool mul_overflow(long long& result, long long operand) {
|
|
||||||
return __builtin_smulll_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool mul_overflow(unsigned int& result, unsigned int operand) {
|
|
||||||
return __builtin_umul_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool mul_overflow(unsigned long& result, unsigned long operand) {
|
|
||||||
return __builtin_umull_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool mul_overflow(unsigned long long& result,
|
|
||||||
unsigned long long operand) {
|
|
||||||
return __builtin_umulll_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////
|
|
||||||
// addition overflow detection
|
|
||||||
////////////////
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
inline bool add_overflow(T& result, T operand) {
|
|
||||||
return __builtin_add_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool add_overflow(int& result, int operand) {
|
|
||||||
return __builtin_sadd_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool add_overflow(long& result, long operand) {
|
|
||||||
return __builtin_saddl_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool add_overflow(long long& result, long long operand) {
|
|
||||||
return __builtin_saddll_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool add_overflow(unsigned int& result, unsigned int operand) {
|
|
||||||
return __builtin_uadd_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool add_overflow(unsigned long& result, unsigned long operand) {
|
|
||||||
return __builtin_uaddl_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool add_overflow(unsigned long long& result,
|
|
||||||
unsigned long long operand) {
|
|
||||||
return __builtin_uaddll_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////
|
|
||||||
// substraction overflow detection
|
|
||||||
////////////////
|
|
||||||
template <typename T>
|
|
||||||
inline bool sub_overflow(T& result, T operand) {
|
|
||||||
return __builtin_sub_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool sub_overflow(int& result, int operand) {
|
|
||||||
return __builtin_ssub_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool sub_overflow(long& result, long operand) {
|
|
||||||
return __builtin_ssubl_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool sub_overflow(long long& result, long long operand) {
|
|
||||||
return __builtin_ssubll_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note: sub_overflow function should be unreachable for unsigned values
|
|
||||||
template <>
|
|
||||||
inline bool sub_overflow(unsigned int& result, unsigned int operand) {
|
|
||||||
return __builtin_usub_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool sub_overflow(unsigned long& result, unsigned long operand) {
|
|
||||||
return __builtin_usubl_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline bool sub_overflow(unsigned long long& result,
|
|
||||||
unsigned long long operand) {
|
|
||||||
return __builtin_usubll_overflow(result, operand, &result);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, typename F>
|
|
||||||
bool shift_and_add_overflow(T& value, T digit, F add_last_digit_overflow) {
|
|
||||||
if (mul_overflow<T>(value, 10) || add_last_digit_overflow(value, digit)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
|
|
||||||
template <typename T, typename U>
|
|
||||||
bool shift_and_add_overflow(T& value, T digit, U is_negative) {
|
|
||||||
digit = (is_negative) ? -digit : digit;
|
|
||||||
T old_value = value;
|
|
||||||
value = 10 * value + digit;
|
|
||||||
|
|
||||||
T expected_old_value = (value - digit) / 10;
|
|
||||||
if (old_value != expected_old_value) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::enable_if_t<std::is_integral_v<T>, std::optional<T>> to_num(
|
std::enable_if_t<std::is_integral_v<T>, std::optional<T>> to_num(
|
||||||
const char* begin, const char* end) {
|
const char* const begin, const char* const end) {
|
||||||
if (begin == end) {
|
T ret;
|
||||||
|
auto [ptr, ec] = std::from_chars(begin, end, ret);
|
||||||
|
|
||||||
|
if (ec != std::errc() || ptr != end) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
bool is_negative = false;
|
return ret;
|
||||||
if constexpr (std::is_signed_v<T>) {
|
|
||||||
is_negative = *begin == '-';
|
|
||||||
if (is_negative) {
|
|
||||||
++begin;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if (defined(__clang__) || defined(__GNUC__) || defined(__GUNG__)) && \
|
|
||||||
!defined(MINGW32_CLANG)
|
|
||||||
auto add_last_digit_overflow =
|
|
||||||
(is_negative) ? sub_overflow<T> : add_overflow<T>;
|
|
||||||
#else
|
|
||||||
auto add_last_digit_overflow = is_negative;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
T value = 0;
|
|
||||||
for (auto i = begin; i != end; ++i) {
|
|
||||||
if (auto digit = from_char(*i);
|
|
||||||
!digit || shift_and_add_overflow<T>(value, digit.value(),
|
|
||||||
add_last_digit_overflow)) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////
|
////////////////
|
||||||
@@ -2196,7 +2021,6 @@ private:
|
|||||||
return extract_tuple<Ts...>(elems);
|
return extract_tuple<Ts...>(elems);
|
||||||
}
|
}
|
||||||
|
|
||||||
// do not know how to specialize by return type :(
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
no_void_validator_tup_t<std::tuple<Ts...>> convert_impl(
|
no_void_validator_tup_t<std::tuple<Ts...>> convert_impl(
|
||||||
const split_data& elems, const std::tuple<Ts...>*) {
|
const split_data& elems, const std::tuple<Ts...>*) {
|
||||||
@@ -2312,7 +2136,6 @@ private:
|
|||||||
|
|
||||||
} /* ss */
|
} /* ss */
|
||||||
|
|
||||||
// TODO add single header tests
|
|
||||||
|
|
||||||
namespace ss {
|
namespace ss {
|
||||||
|
|
||||||
@@ -2458,7 +2281,7 @@ public:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (header_.empty()) {
|
if (header_.empty() && !eof()) {
|
||||||
split_header_data();
|
split_header_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,9 +32,11 @@ set(DOCTEST "${FETCHCONTENT_BASE_DIR}/doctest-src")
|
|||||||
|
|
||||||
enable_testing()
|
enable_testing()
|
||||||
|
|
||||||
foreach(name IN ITEMS test_splitter test_parser test_converter test_extractions
|
foreach(name IN ITEMS test_splitter test_parser1_1 test_parser1_2
|
||||||
test_parser2_1 test_parser2_2 test_parser2_3
|
test_parser1_3 test_parser1_4 test_converter
|
||||||
test_parser2_4 test_extractions_without_fast_float)
|
test_extractions test_parser2_1 test_parser2_2
|
||||||
|
test_parser2_3 test_parser2_4
|
||||||
|
test_extractions_without_fast_float)
|
||||||
add_executable("${name}" "${name}.cpp")
|
add_executable("${name}" "${name}.cpp")
|
||||||
target_link_libraries("${name}" PRIVATE ssp::ssp fast_float
|
target_link_libraries("${name}" PRIVATE ssp::ssp fast_float
|
||||||
doctest::doctest)
|
doctest::doctest)
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ doctest_dep = dependency('doctest')
|
|||||||
add_project_arguments('-DDOCTEST_CONFIG_IMPLEMENT_WITH_MAIN', language: 'cpp')
|
add_project_arguments('-DDOCTEST_CONFIG_IMPLEMENT_WITH_MAIN', language: 'cpp')
|
||||||
|
|
||||||
tests = [
|
tests = [
|
||||||
'parser',
|
'parser1_1',
|
||||||
|
'parser1_2',
|
||||||
|
'parser1_3',
|
||||||
|
'parser1_4',
|
||||||
'splitter',
|
'splitter',
|
||||||
'converter',
|
'converter',
|
||||||
'extractions',
|
'extractions',
|
||||||
|
|||||||
@@ -275,3 +275,20 @@ TEST_CASE("extract test functions for std::variant") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("extract test with long number string") {
|
||||||
|
{
|
||||||
|
std::string string_num =
|
||||||
|
std::string(20, '1') + "." + std::string(20, '2');
|
||||||
|
|
||||||
|
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, float, stof);
|
||||||
|
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, double, stod);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::string string_num =
|
||||||
|
std::string(50, '1') + "." + std::string(50, '2');
|
||||||
|
|
||||||
|
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, double, stod);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ TEST_CASE(
|
|||||||
CHECK_FLOATING_CONVERSION(123.456, float);
|
CHECK_FLOATING_CONVERSION(123.456, float);
|
||||||
CHECK_FLOATING_CONVERSION(123.456, double);
|
CHECK_FLOATING_CONVERSION(123.456, double);
|
||||||
|
|
||||||
CHECK_FLOATING_CONVERSION(69, float);
|
CHECK_FLOATING_CONVERSION(59, float);
|
||||||
CHECK_FLOATING_CONVERSION(69, double);
|
CHECK_FLOATING_CONVERSION(59, double);
|
||||||
|
|
||||||
CHECK_FLOATING_CONVERSION(420., float);
|
CHECK_FLOATING_CONVERSION(4210., float);
|
||||||
CHECK_FLOATING_CONVERSION(420., double);
|
CHECK_FLOATING_CONVERSION(4210., double);
|
||||||
|
|
||||||
CHECK_FLOATING_CONVERSION(0.123, float);
|
CHECK_FLOATING_CONVERSION(0.123, float);
|
||||||
CHECK_FLOATING_CONVERSION(0.123, double);
|
CHECK_FLOATING_CONVERSION(0.123, double);
|
||||||
@@ -130,3 +130,20 @@ TEST_CASE("extract test functions for std::variant without fast float") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("extract test with long number string without fast float") {
|
||||||
|
{
|
||||||
|
std::string string_num =
|
||||||
|
std::string(20, '1') + "." + std::string(20, '2');
|
||||||
|
|
||||||
|
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, float, stof);
|
||||||
|
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, double, stod);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::string string_num =
|
||||||
|
std::string(50, '1') + "." + std::string(50, '2');
|
||||||
|
|
||||||
|
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, double, stod);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <filesystem>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <filesystem>
|
|
||||||
|
|
||||||
#ifdef CMAKE_GITHUB_CI
|
#ifdef CMAKE_GITHUB_CI
|
||||||
#include <doctest/doctest.h>
|
#include <doctest/doctest.h>
|
||||||
@@ -34,23 +34,13 @@ struct buffer {
|
|||||||
|
|
||||||
[[maybe_unused]] inline buffer buff;
|
[[maybe_unused]] inline buffer buff;
|
||||||
|
|
||||||
std::string time_now_rand() {
|
|
||||||
std::stringstream ss;
|
|
||||||
auto t = std::time(nullptr);
|
|
||||||
auto tm = *std::localtime(&t);
|
|
||||||
ss << std::put_time(&tm, "%d%m%Y%H%M%S");
|
|
||||||
srand(time(nullptr));
|
|
||||||
return ss.str() + std::to_string(rand());
|
|
||||||
}
|
|
||||||
|
|
||||||
struct unique_file_name {
|
struct unique_file_name {
|
||||||
static inline int i = 0;
|
std::string name;
|
||||||
|
|
||||||
const std::string name;
|
unique_file_name(const std::string& test) {
|
||||||
|
do {
|
||||||
unique_file_name(const std::string& test)
|
name = std::tmpnam(nullptr) + std::string{"_random_test_"} + test;
|
||||||
: name{"random_" + test + "_" + std::to_string(i++) + "_" +
|
} while (std::filesystem::exists(name));
|
||||||
time_now_rand() + "_file.csv"} {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~unique_file_name() {
|
~unique_file_name() {
|
||||||
@@ -75,6 +65,18 @@ struct unique_file_name {
|
|||||||
CHECK_LT(std::abs(t.value() - type(-input)), eps); \
|
CHECK_LT(std::abs(t.value() - type(-input)), eps); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define CHECK_FLOATING_CONVERSION_LONG_NUMBER(STRING_NUMBER, TYPE, CONVERTER) \
|
||||||
|
{ \
|
||||||
|
auto begin = STRING_NUMBER.c_str(); \
|
||||||
|
auto end = begin + STRING_NUMBER.size(); \
|
||||||
|
\
|
||||||
|
auto number = ss::to_num<TYPE>(begin, end); \
|
||||||
|
REQUIRE(number.has_value()); \
|
||||||
|
\
|
||||||
|
auto expected_number = CONVERTER(STRING_NUMBER); \
|
||||||
|
CHECK_EQ(number.value(), expected_number); \
|
||||||
|
}
|
||||||
|
|
||||||
#define CHECK_INVALID_CONVERSION(input, type) \
|
#define CHECK_INVALID_CONVERSION(input, type) \
|
||||||
{ \
|
{ \
|
||||||
std::string s = input; \
|
std::string s = input; \
|
||||||
|
|||||||
1584
test/test_parser.cpp
1584
test/test_parser.cpp
File diff suppressed because it is too large
Load Diff
151
test/test_parser1.hpp
Normal file
151
test/test_parser1.hpp
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "test_helpers.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <iostream>
|
||||||
|
#include <ss/parser.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
[[maybe_unused]] void replace_all(std::string& s, const std::string& from,
|
||||||
|
const std::string& to) {
|
||||||
|
if (from.empty()) return;
|
||||||
|
size_t start_pos = 0;
|
||||||
|
while ((start_pos = s.find(from, start_pos)) != std::string::npos) {
|
||||||
|
s.replace(start_pos, from.length(), to);
|
||||||
|
start_pos += to.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
void expect_error_on_command(ss::parser<Ts...>& p,
|
||||||
|
const std::function<void()> command) {
|
||||||
|
if (ss::setup<Ts...>::throw_on_error) {
|
||||||
|
try {
|
||||||
|
command();
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
CHECK_FALSE(std::string{e.what()}.empty());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
command();
|
||||||
|
CHECK(!p.valid());
|
||||||
|
if constexpr (ss::setup<Ts...>::string_error) {
|
||||||
|
CHECK_FALSE(p.error_msg().empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] void update_if_crlf(std::string& s) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
replace_all(s, "\r\n", "\n");
|
||||||
|
#else
|
||||||
|
(void)(s);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
struct X {
|
||||||
|
constexpr static auto delim = ",";
|
||||||
|
constexpr static auto empty = "_EMPTY_";
|
||||||
|
int i;
|
||||||
|
double d;
|
||||||
|
std::string s;
|
||||||
|
|
||||||
|
std::string to_string() const {
|
||||||
|
if (s == empty) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::to_string(i)
|
||||||
|
.append(delim)
|
||||||
|
.append(std::to_string(d))
|
||||||
|
.append(delim)
|
||||||
|
.append(s);
|
||||||
|
}
|
||||||
|
auto tied() const {
|
||||||
|
return std::tie(i, d, s);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::enable_if_t<ss::has_m_tied_t<T>, bool> operator==(const T& lhs,
|
||||||
|
const T& rhs) {
|
||||||
|
return lhs.tied() == rhs.tied();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static void make_and_write(const std::string& file_name,
|
||||||
|
const std::vector<T>& data,
|
||||||
|
const std::vector<std::string>& header = {}) {
|
||||||
|
std::ofstream out{file_name};
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
std::vector<const char*> new_lines = {"\n"};
|
||||||
|
#else
|
||||||
|
std::vector<const char*> new_lines = {"\n", "\r\n"};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (const auto& i : header) {
|
||||||
|
if (&i != &header.front()) {
|
||||||
|
out << T::delim;
|
||||||
|
}
|
||||||
|
out << i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!header.empty()) {
|
||||||
|
out << new_lines.front();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < data.size(); ++i) {
|
||||||
|
out << data[i].to_string() << new_lines[i % new_lines.size()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string make_buffer(const std::string& file_name) {
|
||||||
|
std::ifstream in{file_name, std::ios::binary};
|
||||||
|
std::string tmp;
|
||||||
|
std::string out;
|
||||||
|
out.reserve(sizeof(out) + 1);
|
||||||
|
while (in >> tmp) {
|
||||||
|
out += tmp;
|
||||||
|
std::string matches = "\n\r\t ";
|
||||||
|
while (std::any_of(matches.begin(), matches.end(),
|
||||||
|
[&](auto c) { return in.peek() == c; })) {
|
||||||
|
if (in.peek() == '\r') {
|
||||||
|
out += "\r\n";
|
||||||
|
in.ignore(2);
|
||||||
|
} else {
|
||||||
|
out += std::string{static_cast<char>(in.peek())};
|
||||||
|
in.ignore(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <bool buffer_mode, typename... Ts>
|
||||||
|
std::tuple<ss::parser<Ts...>, std::string> make_parser(
|
||||||
|
const std::string& file_name, const std::string& delim = "") {
|
||||||
|
if (buffer_mode) {
|
||||||
|
auto buffer = make_buffer(file_name);
|
||||||
|
if (delim.empty()) {
|
||||||
|
return {ss::parser<Ts...>{buffer.data(), buffer.size()},
|
||||||
|
std::move(buffer)};
|
||||||
|
} else {
|
||||||
|
return {ss::parser<Ts...>{buffer.data(), buffer.size(), delim},
|
||||||
|
std::move(buffer)};
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (delim.empty()) {
|
||||||
|
return {ss::parser<Ts...>{file_name}, std::string{}};
|
||||||
|
} else {
|
||||||
|
return {ss::parser<Ts...>{file_name, delim}, std::string{}};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} /* namespace */
|
||||||
534
test/test_parser1_1.cpp
Normal file
534
test/test_parser1_1.cpp
Normal file
@@ -0,0 +1,534 @@
|
|||||||
|
#include "test_parser1.hpp"
|
||||||
|
|
||||||
|
TEST_CASE("test file not found") {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser p{f.name, ","};
|
||||||
|
CHECK_FALSE(p.valid());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<ss::string_error> p{f.name, ","};
|
||||||
|
CHECK_FALSE(p.valid());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
ss::parser<ss::throw_on_error> p{f.name, ","};
|
||||||
|
FAIL("Expected exception...");
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
CHECK_FALSE(std::string{e.what()}.empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <bool buffer_mode, typename... Ts>
|
||||||
|
void test_various_cases() {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"},
|
||||||
|
{7, 8, "u"}, {9, 10, "v"}, {11, 12, "w"}};
|
||||||
|
make_and_write(f.name, data);
|
||||||
|
auto csv_data_buffer = make_buffer(f.name);
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
ss::parser p0{std::move(p)};
|
||||||
|
p = std::move(p0);
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i2;
|
||||||
|
|
||||||
|
auto move_rotate = [&p = p, &p0 = p0] {
|
||||||
|
auto p1 = std::move(p);
|
||||||
|
p0 = std::move(p1);
|
||||||
|
p = std::move(p0);
|
||||||
|
};
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
move_rotate();
|
||||||
|
auto a = p.template get_next<int, double, std::string>();
|
||||||
|
i.emplace_back(ss::to_object<X>(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& a : p2.template iterate<int, double, std::string>()) {
|
||||||
|
i2.emplace_back(ss::to_object<X>(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
CHECK_EQ(i2, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i2;
|
||||||
|
|
||||||
|
auto [p3, ___] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i3;
|
||||||
|
|
||||||
|
std::vector<X> expected = {std::begin(data) + 1, std::end(data)};
|
||||||
|
using tup = std::tuple<int, double, std::string>;
|
||||||
|
|
||||||
|
p.ignore_next();
|
||||||
|
while (!p.eof()) {
|
||||||
|
auto a = p.template get_next<tup>();
|
||||||
|
i.emplace_back(ss::to_object<X>(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
p2.ignore_next();
|
||||||
|
for (const auto& a : p2.template iterate<tup>()) {
|
||||||
|
i2.emplace_back(ss::to_object<X>(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
p3.ignore_next();
|
||||||
|
for (auto it = p3.template iterate<tup>().begin();
|
||||||
|
it != p3.template iterate<tup>().end(); ++it) {
|
||||||
|
i3.emplace_back(ss::to_object<X>(*it));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, expected);
|
||||||
|
CHECK_EQ(i2, expected);
|
||||||
|
CHECK_EQ(i3, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i;
|
||||||
|
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i2;
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
i.push_back(p.template get_object<X, int, double, std::string>());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto&& a :
|
||||||
|
p2.template iterate_object<X, int, double, std::string>()) {
|
||||||
|
i2.push_back(std::move(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
CHECK_EQ(i2, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
for (auto&& a :
|
||||||
|
p.template iterate_object<X, int, double, std::string>()) {
|
||||||
|
i.push_back(std::move(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i2;
|
||||||
|
|
||||||
|
using tup = std::tuple<int, double, std::string>;
|
||||||
|
while (!p.eof()) {
|
||||||
|
i.push_back(p.template get_object<X, tup>());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto it = p2.template iterate_object<X, tup>().begin();
|
||||||
|
it != p2.template iterate_object<X, tup>().end(); it++) {
|
||||||
|
i2.push_back({it->i, it->d, it->s});
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
CHECK_EQ(i2, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
using tup = std::tuple<int, double, std::string>;
|
||||||
|
for (auto&& a : p.template iterate_object<X, tup>()) {
|
||||||
|
i.push_back(std::move(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
i.push_back(p.template get_next<X>());
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
for (auto&& a : p.template iterate<X>()) {
|
||||||
|
i.push_back(std::move(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr int excluded = 3;
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i2;
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
try {
|
||||||
|
auto a = p.template get_object<X, ss::ax<int, excluded>, double,
|
||||||
|
std::string>();
|
||||||
|
if (p.valid()) {
|
||||||
|
i.push_back(a);
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
// ignore
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ss::setup<Ts...>::throw_on_error) {
|
||||||
|
for (auto&& a : p2.template iterate_object<X, ss::ax<int, excluded>,
|
||||||
|
double, std::string>()) {
|
||||||
|
if (p2.valid()) {
|
||||||
|
i2.push_back(std::move(a));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<X> expected;
|
||||||
|
for (auto& x : data) {
|
||||||
|
if (x.i != excluded) {
|
||||||
|
expected.push_back(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::copy_if(data.begin(), data.end(), expected.begin(),
|
||||||
|
[&](const X& x) { return x.i != excluded; });
|
||||||
|
CHECK_EQ(i, expected);
|
||||||
|
|
||||||
|
if (!ss::setup<Ts...>::throw_on_error) {
|
||||||
|
CHECK_EQ(i2, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
std::vector<X> i2;
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
try {
|
||||||
|
auto a = p.template get_object<X, ss::nx<int, 3>, double,
|
||||||
|
std::string>();
|
||||||
|
if (p.valid()) {
|
||||||
|
i.push_back(a);
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ss::setup<Ts...>::throw_on_error) {
|
||||||
|
for (auto&& a : p2.template iterate_object<X, ss::nx<int, 3>,
|
||||||
|
double, std::string>()) {
|
||||||
|
if (p2.valid()) {
|
||||||
|
i2.push_back(std::move(a));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<X> expected = {{3, 4, "y"}};
|
||||||
|
CHECK_EQ(i, expected);
|
||||||
|
if (!ss::setup<Ts...>::throw_on_error) {
|
||||||
|
CHECK_EQ(i2, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
unique_file_name empty_f{"test_parser"};
|
||||||
|
std::vector<X> empty_data = {};
|
||||||
|
|
||||||
|
make_and_write(empty_f.name, empty_data);
|
||||||
|
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(empty_f.name, ",");
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
auto [p2, __] = make_parser<buffer_mode, Ts...>(empty_f.name, ",");
|
||||||
|
std::vector<X> i2;
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
i.push_back(p.template get_next<X>());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto&& a : p2.template iterate<X>()) {
|
||||||
|
i2.push_back(std::move(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(i.empty());
|
||||||
|
CHECK(i2.empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test various cases") {
|
||||||
|
test_various_cases<false>();
|
||||||
|
test_various_cases<false, ss::string_error>();
|
||||||
|
test_various_cases<false, ss::throw_on_error>();
|
||||||
|
test_various_cases<true>();
|
||||||
|
test_various_cases<true, ss::string_error>();
|
||||||
|
test_various_cases<true, ss::throw_on_error>();
|
||||||
|
}
|
||||||
|
|
||||||
|
using test_tuple = std::tuple<double, char, double>;
|
||||||
|
struct test_struct {
|
||||||
|
int i;
|
||||||
|
double d;
|
||||||
|
char c;
|
||||||
|
auto tied() {
|
||||||
|
return std::tie(i, d, c);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline void expect_test_struct(const test_struct&) {
|
||||||
|
}
|
||||||
|
|
||||||
|
template <bool buffer_mode, typename... Ts>
|
||||||
|
void test_composite_conversion() {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
{
|
||||||
|
std::ofstream out{f.name};
|
||||||
|
for (auto& i :
|
||||||
|
{"10,a,11.1", "10,20,11.1", "junk", "10,11.1", "1,11.1,a", "junk",
|
||||||
|
"10,junk", "11,junk", "10,11.1,c", "10,20", "10,22.2,f"}) {
|
||||||
|
out << i << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
|
||||||
|
auto fail = [] { FAIL(""); };
|
||||||
|
auto expect_error = [](auto error) { CHECK(!error.empty()); };
|
||||||
|
auto ignore_error = [] {};
|
||||||
|
|
||||||
|
REQUIRE(p.valid());
|
||||||
|
REQUIRE_FALSE(p.eof());
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr static auto expectedData = std::tuple{10, 'a', 11.1};
|
||||||
|
|
||||||
|
auto [d1, d2, d3, d4] =
|
||||||
|
p.template try_next<int, int, double>(fail)
|
||||||
|
.template or_else<test_struct>(fail)
|
||||||
|
.template or_else<int, char, double>(
|
||||||
|
[&](auto&& data) { CHECK_EQ(data, expectedData); })
|
||||||
|
.on_error(fail)
|
||||||
|
.template or_else<test_tuple>(fail)
|
||||||
|
.values();
|
||||||
|
|
||||||
|
REQUIRE(p.valid());
|
||||||
|
REQUIRE_FALSE(d1);
|
||||||
|
REQUIRE_FALSE(d2);
|
||||||
|
REQUIRE(d3);
|
||||||
|
REQUIRE_FALSE(d4);
|
||||||
|
CHECK_EQ(*d3, expectedData);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
REQUIRE(!p.eof());
|
||||||
|
constexpr static auto expectedData = std::tuple{10, 20, 11.1};
|
||||||
|
|
||||||
|
auto [d1, d2, d3, d4] =
|
||||||
|
p.template try_next<int, int, double>(
|
||||||
|
[&](auto& i1, auto i2, double d) {
|
||||||
|
CHECK_EQ(std::tie(i1, i2, d), expectedData);
|
||||||
|
})
|
||||||
|
.on_error(fail)
|
||||||
|
.template or_object<test_struct, int, double, char>(fail)
|
||||||
|
.on_error(fail)
|
||||||
|
.template or_else<test_tuple>(fail)
|
||||||
|
.on_error(fail)
|
||||||
|
.template or_else<int, char, double>(fail)
|
||||||
|
.values();
|
||||||
|
|
||||||
|
REQUIRE(p.valid());
|
||||||
|
REQUIRE(d1);
|
||||||
|
REQUIRE_FALSE(d2);
|
||||||
|
REQUIRE_FALSE(d3);
|
||||||
|
REQUIRE_FALSE(d4);
|
||||||
|
CHECK_EQ(*d1, expectedData);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
REQUIRE(!p.eof());
|
||||||
|
|
||||||
|
auto [d1, d2, d3, d4, d5] =
|
||||||
|
p.template try_object<test_struct, int, double, char>(fail)
|
||||||
|
.on_error(expect_error)
|
||||||
|
.template or_else<int, char, char>(fail)
|
||||||
|
.template or_else<test_struct>(fail)
|
||||||
|
.template or_else<test_tuple>(fail)
|
||||||
|
.template or_else<int, char, double>(fail)
|
||||||
|
.values();
|
||||||
|
|
||||||
|
REQUIRE_FALSE(p.valid());
|
||||||
|
REQUIRE_FALSE(d1);
|
||||||
|
REQUIRE_FALSE(d2);
|
||||||
|
REQUIRE_FALSE(d3);
|
||||||
|
REQUIRE_FALSE(d4);
|
||||||
|
REQUIRE_FALSE(d5);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
REQUIRE(!p.eof());
|
||||||
|
|
||||||
|
auto [d1, d2] =
|
||||||
|
p.template try_next<int, double>([](auto& i, auto& d) {
|
||||||
|
REQUIRE_EQ(std::tie(i, d), std::tuple{10, 11.1});
|
||||||
|
})
|
||||||
|
.template or_else<int, double>([](auto&, auto&) { FAIL(""); })
|
||||||
|
.values();
|
||||||
|
|
||||||
|
REQUIRE(p.valid());
|
||||||
|
REQUIRE(d1);
|
||||||
|
REQUIRE_FALSE(d2);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
REQUIRE(!p.eof());
|
||||||
|
|
||||||
|
auto [d1, d2] =
|
||||||
|
p.template try_next<int, double>([](auto&, auto&) { FAIL(""); })
|
||||||
|
.template or_else<test_struct>(expect_test_struct)
|
||||||
|
.values();
|
||||||
|
|
||||||
|
REQUIRE(p.valid());
|
||||||
|
REQUIRE_FALSE(d1);
|
||||||
|
REQUIRE(d2);
|
||||||
|
CHECK_EQ(d2->tied(), std::tuple{1, 11.1, 'a'});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
REQUIRE(!p.eof());
|
||||||
|
|
||||||
|
auto [d1, d2, d3, d4, d5] =
|
||||||
|
p.template try_next<int, int, double>(fail)
|
||||||
|
.template or_object<test_struct, int, double, char>()
|
||||||
|
.template or_else<test_struct>(expect_test_struct)
|
||||||
|
.template or_else<test_tuple>(fail)
|
||||||
|
.template or_else<std::tuple<int, double>>(fail)
|
||||||
|
.on_error(ignore_error)
|
||||||
|
.on_error(expect_error)
|
||||||
|
.values();
|
||||||
|
|
||||||
|
REQUIRE_FALSE(p.valid());
|
||||||
|
REQUIRE_FALSE(d1);
|
||||||
|
REQUIRE_FALSE(d2);
|
||||||
|
REQUIRE_FALSE(d3);
|
||||||
|
REQUIRE_FALSE(d4);
|
||||||
|
REQUIRE_FALSE(d5);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
REQUIRE(!p.eof());
|
||||||
|
|
||||||
|
auto [d1, d2] =
|
||||||
|
p.template try_next<int, std::optional<int>>()
|
||||||
|
.on_error(ignore_error)
|
||||||
|
.on_error(fail)
|
||||||
|
.template or_else<std::tuple<int, std::string>>(fail)
|
||||||
|
.on_error(ignore_error)
|
||||||
|
.on_error(fail)
|
||||||
|
.on_error(ignore_error)
|
||||||
|
.values();
|
||||||
|
|
||||||
|
REQUIRE(p.valid());
|
||||||
|
REQUIRE(d1);
|
||||||
|
REQUIRE_FALSE(d2);
|
||||||
|
CHECK_EQ(*d1, std::tuple{10, std::nullopt});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
REQUIRE_FALSE(p.eof());
|
||||||
|
|
||||||
|
auto [d1, d2] =
|
||||||
|
p.template try_next<int, std::variant<int, std::string>>()
|
||||||
|
.on_error(fail)
|
||||||
|
.template or_else<std::tuple<int, std::string>>(fail)
|
||||||
|
.on_error(fail)
|
||||||
|
.values();
|
||||||
|
|
||||||
|
REQUIRE(p.valid());
|
||||||
|
REQUIRE(d1);
|
||||||
|
REQUIRE_FALSE(d2);
|
||||||
|
CHECK_EQ(*d1, std::tuple{11, std::variant<int, std::string>{"junk"}});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
REQUIRE(!p.eof());
|
||||||
|
|
||||||
|
auto [d1, d2] = p.template try_object<test_struct, int, double, char>()
|
||||||
|
.template or_else<int>(fail)
|
||||||
|
.values();
|
||||||
|
REQUIRE(p.valid());
|
||||||
|
REQUIRE(d1);
|
||||||
|
REQUIRE_FALSE(d2);
|
||||||
|
CHECK_EQ(d1->tied(), std::tuple{10, 11.1, 'c'});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
REQUIRE_FALSE(p.eof());
|
||||||
|
|
||||||
|
auto [d1, d2, d3, d4] =
|
||||||
|
p.template try_next<int, int>([] { return false; })
|
||||||
|
.template or_else<int, double>([](auto&) { return false; })
|
||||||
|
.template or_else<int, int>()
|
||||||
|
.template or_else<int, int>(fail)
|
||||||
|
.values();
|
||||||
|
|
||||||
|
REQUIRE(p.valid());
|
||||||
|
REQUIRE_FALSE(d1);
|
||||||
|
REQUIRE_FALSE(d2);
|
||||||
|
REQUIRE(d3);
|
||||||
|
REQUIRE_FALSE(d4);
|
||||||
|
CHECK_EQ(d3.value(), std::tuple{10, 20});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
REQUIRE(!p.eof());
|
||||||
|
|
||||||
|
auto [d1, d2, d3, d4] =
|
||||||
|
p.template try_object<test_struct, int, double, char>(
|
||||||
|
[] { return false; })
|
||||||
|
.template or_else<int, double>([](auto&) { return false; })
|
||||||
|
.template or_object<test_struct, int, double, char>()
|
||||||
|
.template or_else<int, int>(fail)
|
||||||
|
.values();
|
||||||
|
|
||||||
|
REQUIRE(p.valid());
|
||||||
|
REQUIRE_FALSE(d1);
|
||||||
|
REQUIRE_FALSE(d2);
|
||||||
|
REQUIRE(d3);
|
||||||
|
REQUIRE_FALSE(d4);
|
||||||
|
CHECK_EQ(d3->tied(), std::tuple{10, 22.2, 'f'});
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(p.eof());
|
||||||
|
}
|
||||||
|
|
||||||
|
// various scenarios
|
||||||
|
TEST_CASE("parser test composite conversion") {
|
||||||
|
test_composite_conversion<false, ss::string_error>();
|
||||||
|
test_composite_conversion<true, ss::string_error>();
|
||||||
|
}
|
||||||
309
test/test_parser1_2.cpp
Normal file
309
test/test_parser1_2.cpp
Normal file
@@ -0,0 +1,309 @@
|
|||||||
|
#include "test_parser1.hpp"
|
||||||
|
|
||||||
|
struct my_string {
|
||||||
|
char* data{nullptr};
|
||||||
|
|
||||||
|
my_string() = default;
|
||||||
|
|
||||||
|
~my_string() {
|
||||||
|
delete[] data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure no object is copied
|
||||||
|
my_string(const my_string&) = delete;
|
||||||
|
my_string& operator=(const my_string&) = delete;
|
||||||
|
|
||||||
|
my_string(my_string&& other) : data{other.data} {
|
||||||
|
other.data = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
my_string& operator=(my_string&& other) {
|
||||||
|
data = other.data;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline bool ss::extract(const char* begin, const char* end, my_string& s) {
|
||||||
|
size_t size = end - begin;
|
||||||
|
s.data = new char[size + 1];
|
||||||
|
strncpy(s.data, begin, size);
|
||||||
|
s.data[size] = '\0';
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct xyz {
|
||||||
|
my_string x;
|
||||||
|
my_string y;
|
||||||
|
my_string z;
|
||||||
|
auto tied() {
|
||||||
|
return std::tie(x, y, z);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <bool buffer_mode, typename... Ts>
|
||||||
|
void test_moving_of_parsed_composite_values() {
|
||||||
|
// to compile is enough
|
||||||
|
return;
|
||||||
|
auto [p, _] = make_parser<buffer_mode, Ts...>("", "");
|
||||||
|
p.template try_next<my_string, my_string, my_string>()
|
||||||
|
.template or_else<my_string, my_string, my_string, my_string>(
|
||||||
|
[](auto&&) {})
|
||||||
|
.template or_else<my_string>([](auto&) {})
|
||||||
|
.template or_else<xyz>([](auto&&) {})
|
||||||
|
.template or_object<xyz, my_string, my_string, my_string>([](auto&&) {})
|
||||||
|
.template or_else<std::tuple<my_string, my_string, my_string>>(
|
||||||
|
[](auto&, auto&, auto&) {});
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test the moving of parsed composite values") {
|
||||||
|
test_moving_of_parsed_composite_values<false>();
|
||||||
|
test_moving_of_parsed_composite_values<false, ss::string_error>();
|
||||||
|
test_moving_of_parsed_composite_values<true>();
|
||||||
|
test_moving_of_parsed_composite_values<true, ss::string_error>();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test error mode") {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
{
|
||||||
|
std::ofstream out{f.name};
|
||||||
|
out << "junk" << std::endl;
|
||||||
|
out << "junk" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<false, ss::string_error>(f.name, ",");
|
||||||
|
|
||||||
|
REQUIRE_FALSE(p.eof());
|
||||||
|
p.get_next<int>();
|
||||||
|
CHECK_FALSE(p.valid());
|
||||||
|
CHECK_FALSE(p.error_msg().empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<true, ss::string_error>(f.name, ",");
|
||||||
|
|
||||||
|
REQUIRE_FALSE(p.eof());
|
||||||
|
p.get_next<int>();
|
||||||
|
CHECK_FALSE(p.valid());
|
||||||
|
CHECK_FALSE(p.error_msg().empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser throw on error mode") {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
{
|
||||||
|
std::ofstream out{f.name};
|
||||||
|
out << "junk" << std::endl;
|
||||||
|
out << "junk" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<false, ss::throw_on_error>(f.name, ",");
|
||||||
|
|
||||||
|
REQUIRE_FALSE(p.eof());
|
||||||
|
try {
|
||||||
|
p.get_next<int>();
|
||||||
|
FAIL("Expected exception...");
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
CHECK_FALSE(std::string{e.what()}.empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [p, _] = make_parser<true, ss::throw_on_error>(f.name, ",");
|
||||||
|
|
||||||
|
REQUIRE_FALSE(p.eof());
|
||||||
|
try {
|
||||||
|
p.get_next<int>();
|
||||||
|
FAIL("Expected exception...");
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
CHECK_FALSE(std::string{e.what()}.empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline std::string no_quote(const std::string& s) {
|
||||||
|
if (!s.empty() && s[0] == '"') {
|
||||||
|
return {std::next(begin(s)), std::prev(end(s))};
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <bool buffer_mode, typename... Ts>
|
||||||
|
void test_quote_multiline() {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
std::vector<X> data = {{1, 2, "\"x\r\nx\nx\""},
|
||||||
|
{3, 4, "\"y\ny\r\ny\""},
|
||||||
|
{5, 6, "\"z\nz\""},
|
||||||
|
{7, 8, "\"u\"\"\""},
|
||||||
|
{9, 10, "v"},
|
||||||
|
{11, 12, "\"w\n\""}};
|
||||||
|
for (auto& [_, __, s] : data) {
|
||||||
|
update_if_crlf(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
make_and_write(f.name, data);
|
||||||
|
for (auto& [_, __, s] : data) {
|
||||||
|
s = no_quote(s);
|
||||||
|
if (s[0] == 'u') {
|
||||||
|
s = "u\"";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto [p, _] =
|
||||||
|
make_parser<buffer_mode, ss::multiline, ss::quote<'"'>, Ts...>(f.name,
|
||||||
|
",");
|
||||||
|
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
auto a = p.template get_next<int, double, std::string>();
|
||||||
|
i.emplace_back(ss::to_object<X>(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& [_, __, s] : i) {
|
||||||
|
update_if_crlf(s);
|
||||||
|
}
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
|
||||||
|
auto [p_no_multiline, __] =
|
||||||
|
make_parser<buffer_mode, ss::quote<'"'>, Ts...>(f.name, ",");
|
||||||
|
while (!p.eof()) {
|
||||||
|
auto command = [&p_no_multiline = p_no_multiline] {
|
||||||
|
p_no_multiline.template get_next<int, double, std::string>();
|
||||||
|
};
|
||||||
|
expect_error_on_command(p_no_multiline, command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test csv on multiple lines with quotes") {
|
||||||
|
test_quote_multiline<false>();
|
||||||
|
test_quote_multiline<false, ss::string_error>();
|
||||||
|
test_quote_multiline<false, ss::throw_on_error>();
|
||||||
|
test_quote_multiline<true>();
|
||||||
|
test_quote_multiline<true, ss::string_error>();
|
||||||
|
test_quote_multiline<true, ss::throw_on_error>();
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline std::string no_escape(std::string& s) {
|
||||||
|
s.erase(std::remove(begin(s), end(s), '\\'), end(s));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <bool buffer_mode, typename... Ts>
|
||||||
|
void test_escape_multiline() {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
std::vector<X> data = {{1, 2, "x\\\nx\\\r\nx"},
|
||||||
|
{5, 6, "z\\\nz\\\nz"},
|
||||||
|
{7, 8, "u"},
|
||||||
|
{3, 4, "y\\\ny\\\ny"},
|
||||||
|
{9, 10, "v\\\\"},
|
||||||
|
{11, 12, "w\\\n"}};
|
||||||
|
for (auto& [_, __, s] : data) {
|
||||||
|
update_if_crlf(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
make_and_write(f.name, data);
|
||||||
|
for (auto& [_, __, s] : data) {
|
||||||
|
s = no_escape(s);
|
||||||
|
if (s == "v") {
|
||||||
|
s = "v\\";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto [p, _] =
|
||||||
|
make_parser<buffer_mode, ss::multiline, ss::escape<'\\'>, Ts...>(f.name,
|
||||||
|
",");
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
auto a = p.template get_next<int, double, std::string>();
|
||||||
|
i.emplace_back(ss::to_object<X>(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& [_, __, s] : i) {
|
||||||
|
update_if_crlf(s);
|
||||||
|
}
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
|
||||||
|
auto [p_no_multiline, __] =
|
||||||
|
make_parser<buffer_mode, ss::escape<'\\'>, Ts...>(f.name, ",");
|
||||||
|
while (!p.eof()) {
|
||||||
|
auto command = [&p_no_multiline = p_no_multiline] {
|
||||||
|
auto a =
|
||||||
|
p_no_multiline.template get_next<int, double, std::string>();
|
||||||
|
};
|
||||||
|
expect_error_on_command(p_no_multiline, command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test csv on multiple lines with escapes") {
|
||||||
|
test_escape_multiline<false>();
|
||||||
|
test_escape_multiline<false, ss::string_error>();
|
||||||
|
test_escape_multiline<false, ss::throw_on_error>();
|
||||||
|
test_escape_multiline<true>();
|
||||||
|
test_escape_multiline<true, ss::string_error>();
|
||||||
|
test_escape_multiline<true, ss::throw_on_error>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <bool buffer_mode, typename... Ts>
|
||||||
|
void test_quote_escape_multiline() {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
{
|
||||||
|
std::ofstream out{f.name};
|
||||||
|
out << "1,2,\"just\\\n\nstrings\"" << std::endl;
|
||||||
|
#ifndef _WIN32
|
||||||
|
out << "3,4,\"just\r\nsome\\\r\n\n\\\nstrings\"" << std::endl;
|
||||||
|
out << "5,6,\"just\\\n\\\r\n\r\n\nstrings" << std::endl;
|
||||||
|
#else
|
||||||
|
out << "3,4,\"just\nsome\\\n\n\\\nstrings\"" << std::endl;
|
||||||
|
out << "5,6,\"just\\\n\\\n\n\nstrings" << std::endl;
|
||||||
|
#endif
|
||||||
|
out << "7,8,\"just strings\"" << std::endl;
|
||||||
|
out << "9,10,just strings" << std::endl;
|
||||||
|
}
|
||||||
|
size_t bad_lines = 1;
|
||||||
|
auto num_errors = 0;
|
||||||
|
|
||||||
|
auto [p, _] = make_parser<buffer_mode, ss::multiline, ss::escape<'\\'>,
|
||||||
|
ss::quote<'"'>, Ts...>(f.name);
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
try {
|
||||||
|
auto a = p.template get_next<int, double, std::string>();
|
||||||
|
if (p.valid()) {
|
||||||
|
i.emplace_back(ss::to_object<X>(a));
|
||||||
|
} else {
|
||||||
|
++num_errors;
|
||||||
|
}
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
++num_errors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(bad_lines == num_errors);
|
||||||
|
|
||||||
|
std::vector<X> data = {{1, 2, "just\n\nstrings"},
|
||||||
|
#ifndef _WIN32
|
||||||
|
{3, 4, "just\r\nsome\r\n\n\nstrings"},
|
||||||
|
#else
|
||||||
|
{3, 4, "just\nsome\n\n\nstrings"},
|
||||||
|
#endif
|
||||||
|
{9, 10, "just strings"}};
|
||||||
|
|
||||||
|
for (auto& [_, __, s] : i) {
|
||||||
|
update_if_crlf(s);
|
||||||
|
}
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test csv on multiple lines with quotes and escapes") {
|
||||||
|
test_quote_escape_multiline<false>();
|
||||||
|
test_quote_escape_multiline<false, ss::string_error>();
|
||||||
|
test_quote_escape_multiline<false, ss::throw_on_error>();
|
||||||
|
test_quote_escape_multiline<true>();
|
||||||
|
test_quote_escape_multiline<true, ss::string_error>();
|
||||||
|
test_quote_escape_multiline<true, ss::throw_on_error>();
|
||||||
|
}
|
||||||
515
test/test_parser1_3.cpp
Normal file
515
test/test_parser1_3.cpp
Normal file
@@ -0,0 +1,515 @@
|
|||||||
|
#include "test_parser1.hpp"
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
void test_multiline_restricted() {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
{
|
||||||
|
std::ofstream out{f.name};
|
||||||
|
out << "1,2,\"just\n\nstrings\"" << std::endl;
|
||||||
|
#ifndef _WIN32
|
||||||
|
out << "3,4,\"ju\n\r\n\nnk\"" << std::endl;
|
||||||
|
out << "5,6,just\\\n\\\r\nstrings" << std::endl;
|
||||||
|
#else
|
||||||
|
out << "3,4,\"ju\n\n\nnk\"" << std::endl;
|
||||||
|
out << "5,6,just\\\n\\\nstrings" << std::endl;
|
||||||
|
#endif
|
||||||
|
out << "7,8,ju\\\n\\\n\\\nnk" << std::endl;
|
||||||
|
out << "9,10,\"just\\\n\nstrings\"" << std::endl;
|
||||||
|
out << "11,12,\"ju\\\n|\n\n\n\n\nk\"" << std::endl;
|
||||||
|
out << "13,14,\"ju\\\n\\\n15,16\"\\\n\\\\\n\nnk\"" << std::endl;
|
||||||
|
out << "17,18,\"ju\\\n\\\n\\\n\\\\\n\nnk\"" << std::endl;
|
||||||
|
out << "19,20,just strings" << std::endl;
|
||||||
|
}
|
||||||
|
auto bad_lines = 15;
|
||||||
|
auto num_errors = 0;
|
||||||
|
|
||||||
|
ss::parser<ss::multiline_restricted<2>, ss::quote<'"'>, ss::escape<'\\'>,
|
||||||
|
Ts...>
|
||||||
|
p{f.name, ","};
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
try {
|
||||||
|
auto a = p.template get_next<int, double, std::string>();
|
||||||
|
if (p.valid()) {
|
||||||
|
i.emplace_back(ss::to_object<X>(a));
|
||||||
|
} else {
|
||||||
|
++num_errors;
|
||||||
|
}
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
++num_errors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(bad_lines == num_errors);
|
||||||
|
|
||||||
|
std::vector<X> data = {{1, 2, "just\n\nstrings"},
|
||||||
|
#ifndef _WIN32
|
||||||
|
{5, 6, "just\n\r\nstrings"},
|
||||||
|
#else
|
||||||
|
{5, 6, "just\n\nstrings"},
|
||||||
|
#endif
|
||||||
|
{9, 10, "just\n\nstrings"},
|
||||||
|
{19, 20, "just strings"}};
|
||||||
|
|
||||||
|
for (auto& [_, __, s] : i) {
|
||||||
|
update_if_crlf(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i.size() != data.size()) {
|
||||||
|
CHECK_EQ(i.size(), data.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test multiline restricted") {
|
||||||
|
test_multiline_restricted();
|
||||||
|
test_multiline_restricted<ss::string_error>();
|
||||||
|
test_multiline_restricted<ss::throw_on_error>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
void test_unterminated_line_impl(const std::vector<std::string>& lines,
|
||||||
|
size_t bad_line) {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
std::ofstream out{f.name};
|
||||||
|
for (const auto& line : lines) {
|
||||||
|
out << line << std::endl;
|
||||||
|
}
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
ss::parser<Ts...> p{f.name};
|
||||||
|
size_t line = 0;
|
||||||
|
while (!p.eof()) {
|
||||||
|
auto command = [&] { p.template get_next<int, double, std::string>(); };
|
||||||
|
|
||||||
|
if (line == bad_line) {
|
||||||
|
expect_error_on_command(p, command);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
command();
|
||||||
|
CHECK(p.valid());
|
||||||
|
++line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
void test_unterminated_line(const std::vector<std::string>& lines,
|
||||||
|
size_t bad_line) {
|
||||||
|
test_unterminated_line_impl<Ts...>(lines, bad_line);
|
||||||
|
test_unterminated_line_impl<Ts..., ss::string_error>(lines, bad_line);
|
||||||
|
test_unterminated_line_impl<Ts..., ss::throw_on_error>(lines, bad_line);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test csv on multiline with errors") {
|
||||||
|
using multiline = ss::multiline_restricted<3>;
|
||||||
|
using escape = ss::escape<'\\'>;
|
||||||
|
using quote = ss::quote<'"'>;
|
||||||
|
|
||||||
|
// unterminated escape
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"1,2,just\\"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 0);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"1,2,just\\", "9,8,second"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 0);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first", "1,2,just\\"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 1);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first", "1,2,just\\",
|
||||||
|
"3,4,third"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 1);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first",
|
||||||
|
"1,2,just\\\nstrings\\",
|
||||||
|
"3,4,th\\\nird"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 1);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first", "3,4,second",
|
||||||
|
"1,2,just\\"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 2);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,\\first", "3,4,second",
|
||||||
|
"1,2,jus\\t\\"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 2);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// unterminated quote
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"1,2,\"just"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
test_unterminated_line<multiline, quote>(lines, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"1,2,\"just", "9,8,second"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
test_unterminated_line<multiline, quote>(lines, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first", "1,2,\"just"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
test_unterminated_line<multiline, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first", "1,2,\"just",
|
||||||
|
"3,4,th\\,ird"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
test_unterminated_line<multiline, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first", "3,4,second",
|
||||||
|
"1,2,\"just"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 2);
|
||||||
|
test_unterminated_line<multiline, quote>(lines, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,\"first\"",
|
||||||
|
"\"3\",4,\"sec,ond\"",
|
||||||
|
"1,2,\"ju\"\"st"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 2);
|
||||||
|
test_unterminated_line<multiline, quote>(lines, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// unterminated quote and escape
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"1,2,\"just\\"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"1,2,\"just\\\n\\"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"1,2,\"just\n\\"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\",
|
||||||
|
"4,3,thrid"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,f\\\nirst", "1,2,\"just\n\\",
|
||||||
|
"4,3,thrid"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,\"f\ni\nrst\"",
|
||||||
|
"1,2,\"just\n\\", "4,3,thrid"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// multiline limmit reached escape
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"1,2,\\\n\\\n\\\n\\\njust"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 0);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first",
|
||||||
|
"1,2,\\\n\\\n\\\n\\\njust"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 1);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,fi\\\nrs\\\nt",
|
||||||
|
"1,2,\\\n\\\n\\\n\\\njust"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 1);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first",
|
||||||
|
"1,2,\\\n\\\n\\\n\\\njust",
|
||||||
|
"4,3,third"};
|
||||||
|
test_unterminated_line<multiline, escape>(lines, 1);
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// multiline limmit reached quote
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"1,2,\"\n\n\n\n\njust\""};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
test_unterminated_line<multiline, quote>(lines, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first",
|
||||||
|
"1,2,\"\n\n\n\n\njust\""};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
test_unterminated_line<multiline, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,\"fir\nst\"",
|
||||||
|
"1,2,\"\n\n\n\n\njust\""};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
test_unterminated_line<multiline, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// multiline limmit reached quote and escape
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"1,2,\"\\\n\n\\\n\\\n\\\njust"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,first",
|
||||||
|
"1,2,\"\\\n\n\\\n\\\n\\\njust"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,fi\\\nrst",
|
||||||
|
"1,2,\"\\\n\n\\\n\\\n\\\njust"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,\"fi\nrst\"",
|
||||||
|
"1,2,\"\\\n\n\\\n\\\n\\\njust"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::vector<std::string> lines{"9,8,\"fi\nr\\\nst\"",
|
||||||
|
"1,2,\"\\\n\n\\\n\\\n\\\njust"};
|
||||||
|
test_unterminated_line<multiline, escape, quote>(lines, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename Tuple>
|
||||||
|
struct has_type;
|
||||||
|
|
||||||
|
template <typename T, typename... Us>
|
||||||
|
struct has_type<T, std::tuple<Us...>>
|
||||||
|
: std::disjunction<std::is_same<T, Us>...> {};
|
||||||
|
|
||||||
|
static inline void check_size(size_t size1, size_t size2) {
|
||||||
|
CHECK_EQ(size1, size2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Setup, typename... Ts>
|
||||||
|
static void test_fields_impl(const std::string file_name,
|
||||||
|
const std::vector<X>& data,
|
||||||
|
const std::vector<std::string>& fields) {
|
||||||
|
using CaseType = std::tuple<Ts...>;
|
||||||
|
|
||||||
|
ss::parser<Setup> p{file_name, ","};
|
||||||
|
CHECK_FALSE(p.field_exists("Unknown"));
|
||||||
|
p.use_fields(fields);
|
||||||
|
std::vector<CaseType> i;
|
||||||
|
|
||||||
|
for (const auto& a : p.template iterate<CaseType>()) {
|
||||||
|
i.push_back(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
check_size(i.size(), data.size());
|
||||||
|
for (size_t j = 0; j < i.size(); ++j) {
|
||||||
|
if constexpr (has_type<int, CaseType>::value) {
|
||||||
|
CHECK_EQ(std::get<int>(i[j]), data[j].i);
|
||||||
|
}
|
||||||
|
if constexpr (has_type<double, CaseType>::value) {
|
||||||
|
CHECK_EQ(std::get<double>(i[j]), data[j].d);
|
||||||
|
}
|
||||||
|
if constexpr (has_type<std::string, CaseType>::value) {
|
||||||
|
CHECK_EQ(std::get<std::string>(i[j]), data[j].s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
static void test_fields(const std::string file_name, const std::vector<X>& data,
|
||||||
|
const std::vector<std::string>& fields) {
|
||||||
|
test_fields_impl<ss::setup<>, Ts...>(file_name, data, fields);
|
||||||
|
test_fields_impl<ss::setup<ss::string_error>, Ts...>(file_name, data,
|
||||||
|
fields);
|
||||||
|
test_fields_impl<ss::setup<ss::throw_on_error>, Ts...>(file_name, data,
|
||||||
|
fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test various cases with header") {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
constexpr static auto Int = "Int";
|
||||||
|
constexpr static auto Dbl = "Double";
|
||||||
|
constexpr static auto Str = "String";
|
||||||
|
using str = std::string;
|
||||||
|
|
||||||
|
std::vector<std::string> header{Int, Dbl, Str};
|
||||||
|
|
||||||
|
std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"},
|
||||||
|
{7, 8, "u"}, {9, 10, "v"}, {11, 12, "w"}};
|
||||||
|
|
||||||
|
make_and_write(f.name, data, header);
|
||||||
|
const auto& o = f.name;
|
||||||
|
const auto& d = data;
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<ss::string_error> p{f.name, ","};
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
for (const auto& a : p.iterate<int, double, std::string>()) {
|
||||||
|
i.emplace_back(ss::to_object<X>(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_NE(i, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<ss::string_error> p{f.name, ","};
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
p.ignore_next();
|
||||||
|
for (const auto& a : p.iterate<int, double, std::string>()) {
|
||||||
|
i.emplace_back(ss::to_object<X>(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<ss::ignore_header> p{f.name, ","};
|
||||||
|
std::vector<X> i;
|
||||||
|
|
||||||
|
for (const auto& a : p.iterate<int, double, std::string>()) {
|
||||||
|
i.emplace_back(ss::to_object<X>(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<ss::ignore_header, ss::string_error> p{f.name, ","};
|
||||||
|
p.use_fields(Int, Dbl, Str);
|
||||||
|
CHECK_FALSE(p.valid());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<ss::ignore_header, ss::string_error> p{f.name, ","};
|
||||||
|
CHECK_FALSE(p.field_exists("Unknown"));
|
||||||
|
|
||||||
|
p.use_fields(Int, "Unknown");
|
||||||
|
CHECK_FALSE(p.valid());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<ss::ignore_header, ss::string_error> p{f.name, ","};
|
||||||
|
p.use_fields(Int, Int);
|
||||||
|
CHECK_FALSE(p.valid());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<ss::string_error> p{f.name, ","};
|
||||||
|
p.use_fields(Int, Dbl);
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [int_, double_] = p.get_next<int, double>();
|
||||||
|
CHECK_EQ(int_, data[0].i);
|
||||||
|
CHECK_EQ(double_, data[0].d);
|
||||||
|
}
|
||||||
|
|
||||||
|
p.use_fields(Dbl, Int);
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [double_, int_] = p.get_next<double, int>();
|
||||||
|
CHECK_EQ(int_, data[1].i);
|
||||||
|
CHECK_EQ(double_, data[1].d);
|
||||||
|
}
|
||||||
|
|
||||||
|
p.use_fields(Str);
|
||||||
|
|
||||||
|
{
|
||||||
|
auto string_ = p.get_next<std::string>();
|
||||||
|
CHECK_EQ(string_, data[2].s);
|
||||||
|
}
|
||||||
|
|
||||||
|
p.use_fields(Str, Int, Dbl);
|
||||||
|
|
||||||
|
{
|
||||||
|
auto [string_, int_, double_] =
|
||||||
|
p.get_next<std::string, int, double>();
|
||||||
|
CHECK_EQ(double_, data[3].d);
|
||||||
|
CHECK_EQ(int_, data[3].i);
|
||||||
|
CHECK_EQ(string_, data[3].s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* python used to generate permutations
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
header = {'str': 'Str',
|
||||||
|
'double': 'Dbl',
|
||||||
|
'int': 'Int'}
|
||||||
|
|
||||||
|
keys = ['str', 'int', 'double']
|
||||||
|
|
||||||
|
for r in range (1, 3):
|
||||||
|
combinations = list(itertools.permutations(keys, r = r))
|
||||||
|
|
||||||
|
for combination in combinations:
|
||||||
|
template_params = []
|
||||||
|
arg_params = []
|
||||||
|
for type in combination:
|
||||||
|
template_params.append(type)
|
||||||
|
arg_params.append(header[type])
|
||||||
|
call = 'testFields<' + ', '.join(template_params) + \
|
||||||
|
'>(o, d, {' + ', '.join(arg_params) + '});'
|
||||||
|
print(call)
|
||||||
|
*/
|
||||||
|
|
||||||
|
test_fields<str>(o, d, {Str});
|
||||||
|
test_fields<int>(o, d, {Int});
|
||||||
|
test_fields<double>(o, d, {Dbl});
|
||||||
|
test_fields<str, int>(o, d, {Str, Int});
|
||||||
|
test_fields<str, double>(o, d, {Str, Dbl});
|
||||||
|
test_fields<int, str>(o, d, {Int, Str});
|
||||||
|
test_fields<int, double>(o, d, {Int, Dbl});
|
||||||
|
test_fields<double, str>(o, d, {Dbl, Str});
|
||||||
|
test_fields<double, int>(o, d, {Dbl, Int});
|
||||||
|
test_fields<str, int, double>(o, d, {Str, Int, Dbl});
|
||||||
|
test_fields<str, double, int>(o, d, {Str, Dbl, Int});
|
||||||
|
test_fields<int, str, double>(o, d, {Int, Str, Dbl});
|
||||||
|
test_fields<int, double, str>(o, d, {Int, Dbl, Str});
|
||||||
|
test_fields<double, str, int>(o, d, {Dbl, Str, Int});
|
||||||
|
test_fields<double, int, str>(o, d, {Dbl, Int, Str});
|
||||||
|
}
|
||||||
291
test/test_parser1_4.cpp
Normal file
291
test/test_parser1_4.cpp
Normal file
@@ -0,0 +1,291 @@
|
|||||||
|
#include "test_parser1.hpp"
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
void test_invalid_fields_impl(const std::vector<std::string>& lines,
|
||||||
|
const std::vector<std::string>& fields) {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
std::ofstream out{f.name};
|
||||||
|
for (const auto& line : lines) {
|
||||||
|
out << line << std::endl;
|
||||||
|
}
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
{
|
||||||
|
// No fields specified
|
||||||
|
ss::parser<Ts...> p{f.name, ","};
|
||||||
|
auto command = [&] { p.use_fields(); };
|
||||||
|
expect_error_on_command(p, command);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Unknown field
|
||||||
|
ss::parser<Ts...> p{f.name, ","};
|
||||||
|
auto command = [&] { p.use_fields("Unknown"); };
|
||||||
|
expect_error_on_command(p, command);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Field used multiple times
|
||||||
|
ss::parser<Ts...> p{f.name, ","};
|
||||||
|
auto command = [&] { p.use_fields(fields.at(0), fields.at(0)); };
|
||||||
|
if (!fields.empty()) {
|
||||||
|
expect_error_on_command(p, command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Mapping out of range
|
||||||
|
ss::parser<Ts...> p{f.name, ","};
|
||||||
|
auto command = [&] {
|
||||||
|
p.use_fields(fields.at(0));
|
||||||
|
p.template get_next<std::string, std::string>();
|
||||||
|
};
|
||||||
|
if (!fields.empty()) {
|
||||||
|
expect_error_on_command(p, command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Invalid header
|
||||||
|
ss::parser<Ts...> p{f.name, ","};
|
||||||
|
auto command = [&] { p.use_fields(fields); };
|
||||||
|
|
||||||
|
if (!fields.empty()) {
|
||||||
|
// Pass if there are no duplicates, fail otherwise
|
||||||
|
if (std::unordered_set<std::string>{fields.begin(), fields.end()}
|
||||||
|
.size() != fields.size()) {
|
||||||
|
expect_error_on_command(p, command);
|
||||||
|
} else {
|
||||||
|
command();
|
||||||
|
CHECK(p.valid());
|
||||||
|
if (!p.valid()) {
|
||||||
|
if constexpr (ss::setup<Ts...>::string_error) {
|
||||||
|
std::cout << p.error_msg() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
void test_invalid_fields(const std::vector<std::string>& lines,
|
||||||
|
const std::vector<std::string>& fields) {
|
||||||
|
test_invalid_fields_impl(lines, fields);
|
||||||
|
test_invalid_fields_impl<ss::string_error>(lines, fields);
|
||||||
|
test_invalid_fields_impl<ss::throw_on_error>(lines, fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test invalid header fields usage") {
|
||||||
|
test_invalid_fields({}, {});
|
||||||
|
|
||||||
|
test_invalid_fields({"Int"}, {"Int"});
|
||||||
|
test_invalid_fields({"Int", "1"}, {"Int"});
|
||||||
|
test_invalid_fields({"Int", "1", "2"}, {"Int"});
|
||||||
|
|
||||||
|
test_invalid_fields({"Int,String"}, {"Int", "String"});
|
||||||
|
test_invalid_fields({"Int,String", "1,hi"}, {"Int", "String"});
|
||||||
|
test_invalid_fields({"Int,String", "2,hello"}, {"Int", "String"});
|
||||||
|
|
||||||
|
test_invalid_fields({"Int,String,Double"}, {"Int", "String", "Double"});
|
||||||
|
test_invalid_fields({"Int,String,Double", "1,hi,2.34"},
|
||||||
|
{"Int", "String", "Double"});
|
||||||
|
test_invalid_fields({"Int,String,Double", "1,hi,2.34", "2,hello,3.45"},
|
||||||
|
{"Int", "String", "Double"});
|
||||||
|
|
||||||
|
test_invalid_fields({"Int,Int,Int"}, {"Int", "Int", "Int"});
|
||||||
|
test_invalid_fields({"Int,Int,Int", "1,2,3"}, {"Int", "Int", "Int"});
|
||||||
|
|
||||||
|
test_invalid_fields({"Int,String,Int"}, {"Int", "String", "Int"});
|
||||||
|
test_invalid_fields({"Int,String,Int", "1,hi,3"}, {"Int", "String", "Int"});
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
void test_invalid_rows_with_header() {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
{
|
||||||
|
std::ofstream out{f.name};
|
||||||
|
out << "Int,String,Double" << std::endl;
|
||||||
|
out << "1,line1,2.34" << std::endl;
|
||||||
|
out << "2,line2" << std::endl;
|
||||||
|
out << "3,line3,67.8" << std::endl;
|
||||||
|
out << "4,line4,67.8,9" << std::endl;
|
||||||
|
out << "5,line5,9.10" << std::endl;
|
||||||
|
out << "six,line6,10.11" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<Ts...> p{f.name};
|
||||||
|
|
||||||
|
p.use_fields("Int", "String", "Double");
|
||||||
|
using data = std::tuple<int, std::string, double>;
|
||||||
|
std::vector<data> i;
|
||||||
|
|
||||||
|
CHECK(p.valid());
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
try {
|
||||||
|
const auto& t = p.template get_next<data>();
|
||||||
|
if (p.valid()) {
|
||||||
|
i.push_back(t);
|
||||||
|
}
|
||||||
|
} catch (const ss::exception&) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<data> expected = {{1, "line1", 2.34},
|
||||||
|
{3, "line3", 67.8},
|
||||||
|
{5, "line5", 9.10}};
|
||||||
|
CHECK_EQ(i, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<Ts...> p{f.name};
|
||||||
|
|
||||||
|
p.use_fields("Double", "Int");
|
||||||
|
using data = std::tuple<double, int>;
|
||||||
|
std::vector<data> i;
|
||||||
|
|
||||||
|
CHECK(p.valid());
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
try {
|
||||||
|
const auto& t = p.template get_next<data>();
|
||||||
|
if (p.valid()) {
|
||||||
|
i.push_back(t);
|
||||||
|
}
|
||||||
|
} catch (const ss::exception&) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<data> expected = {{2.34, 1}, {67.8, 3}, {9.10, 5}};
|
||||||
|
CHECK_EQ(i, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<Ts...> p{f.name};
|
||||||
|
|
||||||
|
p.use_fields("String", "Double");
|
||||||
|
using data = std::tuple<std::string, double>;
|
||||||
|
std::vector<data> i;
|
||||||
|
|
||||||
|
CHECK(p.valid());
|
||||||
|
|
||||||
|
while (!p.eof()) {
|
||||||
|
try {
|
||||||
|
const auto& t = p.template get_next<data>();
|
||||||
|
if (p.valid()) {
|
||||||
|
i.push_back(t);
|
||||||
|
}
|
||||||
|
} catch (const ss::exception&) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<data> expected = {{"line1", 2.34},
|
||||||
|
{"line3", 67.8},
|
||||||
|
{"line5", 9.10},
|
||||||
|
{"line6", 10.11}};
|
||||||
|
CHECK_EQ(i, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test invalid rows with header") {
|
||||||
|
test_invalid_rows_with_header();
|
||||||
|
test_invalid_rows_with_header<ss::string_error>();
|
||||||
|
test_invalid_rows_with_header<ss::throw_on_error>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
void test_ignore_empty_impl(const std::vector<X>& data) {
|
||||||
|
unique_file_name f{"test_parser"};
|
||||||
|
make_and_write(f.name, data);
|
||||||
|
|
||||||
|
std::vector<X> expected;
|
||||||
|
for (const auto& d : data) {
|
||||||
|
if (d.s != X::empty) {
|
||||||
|
expected.push_back(d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<ss::ignore_empty, Ts...> p{f.name, ","};
|
||||||
|
|
||||||
|
std::vector<X> i;
|
||||||
|
for (const auto& a : p.template iterate<X>()) {
|
||||||
|
i.push_back(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::parser<Ts...> p{f.name, ","};
|
||||||
|
std::vector<X> i;
|
||||||
|
size_t n = 0;
|
||||||
|
while (!p.eof()) {
|
||||||
|
try {
|
||||||
|
++n;
|
||||||
|
const auto& a = p.template get_next<X>();
|
||||||
|
if (data.at(n - 1).s == X::empty) {
|
||||||
|
CHECK_FALSE(p.valid());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
i.push_back(a);
|
||||||
|
} catch (...) {
|
||||||
|
CHECK_EQ(data.at(n - 1).s, X::empty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK_EQ(i, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
void test_ignore_empty(const std::vector<X>& data) {
|
||||||
|
test_ignore_empty_impl(data);
|
||||||
|
test_ignore_empty_impl<ss::string_error>(data);
|
||||||
|
test_ignore_empty_impl<ss::throw_on_error>(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parser test various cases with empty lines") {
|
||||||
|
test_ignore_empty({{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}});
|
||||||
|
|
||||||
|
test_ignore_empty(
|
||||||
|
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}});
|
||||||
|
|
||||||
|
test_ignore_empty(
|
||||||
|
{{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}});
|
||||||
|
|
||||||
|
test_ignore_empty(
|
||||||
|
{{1, 2, "x"}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}});
|
||||||
|
|
||||||
|
test_ignore_empty(
|
||||||
|
{{1, 2, X::empty}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}});
|
||||||
|
|
||||||
|
test_ignore_empty(
|
||||||
|
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}});
|
||||||
|
|
||||||
|
test_ignore_empty(
|
||||||
|
{{1, 2, "x"}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}});
|
||||||
|
|
||||||
|
test_ignore_empty(
|
||||||
|
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}});
|
||||||
|
|
||||||
|
test_ignore_empty({{1, 2, X::empty},
|
||||||
|
{3, 4, X::empty},
|
||||||
|
{9, 10, X::empty},
|
||||||
|
{11, 12, X::empty}});
|
||||||
|
|
||||||
|
test_ignore_empty(
|
||||||
|
{{1, 2, "x"}, {3, 4, X::empty}, {9, 10, X::empty}, {11, 12, X::empty}});
|
||||||
|
|
||||||
|
test_ignore_empty(
|
||||||
|
{{1, 2, X::empty}, {3, 4, X::empty}, {9, 10, X::empty}, {11, 12, "w"}});
|
||||||
|
|
||||||
|
test_ignore_empty({{11, 12, X::empty}});
|
||||||
|
|
||||||
|
test_ignore_empty({});
|
||||||
|
}
|
||||||
@@ -775,6 +775,23 @@ TEST_CASE("splitter test resplit unterminated quote") {
|
|||||||
CHECK_EQ(words(vec), expected);
|
CHECK_EQ(words(vec), expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ss::converter<ss::quote<'"'>, ss::escape<'\\'>, ss::multiline> c;
|
||||||
|
auto& s = c.splitter;
|
||||||
|
auto vec = expect_unterminated_quote(s, R"("just\"some","ra)");
|
||||||
|
std::vector<std::string> expected{"just\"some"};
|
||||||
|
auto w = words(vec);
|
||||||
|
w.pop_back();
|
||||||
|
CHECK_EQ(w, expected);
|
||||||
|
REQUIRE(s.unterminated_quote());
|
||||||
|
{
|
||||||
|
auto new_line = buff.append(R"(n,dom",str\"ings)");
|
||||||
|
// invalid resplit size
|
||||||
|
vec = c.resplit(new_line, 4);
|
||||||
|
CHECK(!s.valid());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("splitter test resplit unterminated quote with exceptions") {
|
TEST_CASE("splitter test resplit unterminated quote with exceptions") {
|
||||||
@@ -1040,47 +1057,57 @@ TEST_CASE("splitter test resplit unterminated quote with exceptions") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("splitter test invalid splits") {
|
template <typename... Ts>
|
||||||
ss::converter<ss::string_error, ss::quote<'"'>, ss::trim<' '>,
|
void test_invalid_splits() {
|
||||||
ss::escape<'\\'>>
|
ss::converter<ss::quote<'"'>, ss::trim<' '>, ss::escape<'\\'>, Ts...> c;
|
||||||
c;
|
|
||||||
auto& s = c.splitter;
|
auto& s = c.splitter;
|
||||||
|
|
||||||
|
auto check_error_msg = [&] {
|
||||||
|
if constexpr (ss::setup<Ts...>::string_error) {
|
||||||
|
CHECK_FALSE(s.error_msg().empty());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// empty delimiter
|
// empty delimiter
|
||||||
s.split(buff("some,random,strings"), "");
|
s.split(buff("some,random,strings"), "");
|
||||||
CHECK_FALSE(s.valid());
|
CHECK_FALSE(s.valid());
|
||||||
CHECK_FALSE(s.unterminated_quote());
|
CHECK_FALSE(s.unterminated_quote());
|
||||||
CHECK_FALSE(s.error_msg().empty());
|
check_error_msg();
|
||||||
|
|
||||||
// mismatched delimiter
|
// mismatched delimiter
|
||||||
s.split(buff(R"(some,"random,"strings")"));
|
s.split(buff(R"(some,"random,"strings")"));
|
||||||
CHECK_FALSE(s.valid());
|
CHECK_FALSE(s.valid());
|
||||||
CHECK_FALSE(s.unterminated_quote());
|
CHECK_FALSE(s.unterminated_quote());
|
||||||
CHECK_FALSE(s.error_msg().empty());
|
check_error_msg();
|
||||||
|
|
||||||
// unterminated escape
|
// unterminated escape
|
||||||
s.split(buff(R"(some,random,strings\)"));
|
s.split(buff(R"(some,random,strings\)"));
|
||||||
CHECK_FALSE(s.valid());
|
CHECK_FALSE(s.valid());
|
||||||
CHECK_FALSE(s.unterminated_quote());
|
CHECK_FALSE(s.unterminated_quote());
|
||||||
CHECK_FALSE(s.error_msg().empty());
|
check_error_msg();
|
||||||
|
|
||||||
// unterminated escape
|
// unterminated escape
|
||||||
s.split(buff(R"(some,random,"strings\)"));
|
s.split(buff(R"(some,random,"strings\)"));
|
||||||
CHECK_FALSE(s.valid());
|
CHECK_FALSE(s.valid());
|
||||||
CHECK_FALSE(s.unterminated_quote());
|
CHECK_FALSE(s.unterminated_quote());
|
||||||
CHECK_FALSE(s.error_msg().empty());
|
check_error_msg();
|
||||||
|
|
||||||
// unterminated quote
|
// unterminated quote
|
||||||
s.split(buff("some,random,\"strings"));
|
s.split(buff("some,random,\"strings"));
|
||||||
CHECK_FALSE(s.valid());
|
CHECK_FALSE(s.valid());
|
||||||
CHECK(s.unterminated_quote());
|
CHECK(s.unterminated_quote());
|
||||||
CHECK_FALSE(s.error_msg().empty());
|
check_error_msg();
|
||||||
|
|
||||||
// invalid resplit
|
// invalid resplit
|
||||||
char new_line[] = "some";
|
char new_line[] = "some";
|
||||||
c.resplit(new_line, strlen(new_line));
|
c.resplit(new_line, strlen(new_line));
|
||||||
CHECK_FALSE(s.valid());
|
CHECK_FALSE(s.valid());
|
||||||
CHECK_FALSE(s.error_msg().empty());
|
check_error_msg();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("splitter test invalid splits") {
|
||||||
|
test_invalid_splits();
|
||||||
|
test_invalid_splits<ss::string_error>();
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("splitter test invalid splits with exceptions") {
|
TEST_CASE("splitter test invalid splits with exceptions") {
|
||||||
|
|||||||
Reference in New Issue
Block a user