File: titles_base.cpp

package info (click to toggle)
btag 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 428 kB
  • sloc: cpp: 1,711; makefile: 13
file content (95 lines) | stat: -rw-r--r-- 2,865 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * This file is part of btag.
 *
 * © 2011-2012 Fernando Tarlá Cardoso Lemos
 *
 * Refer to the LICENSE file for licensing information.
 *
 */

#include <boost/algorithm/string.hpp>
#include <boost/locale.hpp>
#include <cstdlib>
#include <fstream>
#include <iostream>

#include "titles_base.h"

int run_title_capitalization_tests(TitleLocalizationHandler &handler, int argc, char **argv)
{
    // Find the path to the test data
    if (argc != 2) {
        std::cerr << "You need to supply the path to test data file in the command line" << std::endl;
        return EXIT_FAILURE;
    }

    // Open the test data file
    std::wifstream f(argv[1]);
    if (!f.is_open()) {
        std::cerr << "Failed to open the test data file" << std::endl;
        return EXIT_FAILURE;
    }

    // Force an UTF-8 locale
    std::ios_base::sync_with_stdio(false);
    boost::locale::generator gen;
    std::locale locale(gen("en_US.UTF-8"));
    f.imbue(locale);
    std::wcerr.imbue(locale);

    // Set up the capitalization filter
    TitleCapitalizationFilter filter;
    filter.set_localization_handler(&handler);

    int errors = 0, correct_lines = 0, lineno = 1;
    std::wstring line;
    for (;;) {
        // Get a line
        std::getline(f, line);
        if (f.eof())
            break;
        else if (f.fail()) {
            std::wcerr << L"Error reading line from file" << std::endl;
            return EXIT_FAILURE;
        }

        // Check if we can skip it
        std::wstring::size_type first = line.find_first_not_of(L" \t\n\r");
        if (first == std::wstring::npos || line[first] == L'#') {
            ++lineno;
            continue;
        }

        // Trim it
        std::wstring::size_type last = line.find_last_not_of(L" \t\n\r");
        std::wstring correctStr(line.substr(first, last - first + 1));

        // Get lowercase and uppercase versions of the string
        std::wstring lowerStr(boost::algorithm::to_lower_copy(correctStr));
        std::wstring upperStr(boost::algorithm::to_upper_copy(correctStr));

        // Apply the localization handler
        std::wstring lowerFiltered(filter.filter(lowerStr));
        std::wstring upperFiltered(filter.filter(upperStr));

        // Make sure they match
        bool has_error = false;
        if (lowerFiltered != correctStr) {
            std::wcerr << lineno << L": Incorrect conversion from lowercase" << std::endl;
            has_error = true;
            ++errors;
        }
        if (upperFiltered != correctStr) {
            std::wcerr << lineno << L": Incorrect conversion from uppercase" << std::endl;
            has_error = true;
            ++errors;
        }

        // Increment the counters
        ++lineno;
        if (!has_error)
            ++correct_lines;
    }

    return correct_lines > 10 && errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}