Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/unittest/src/utils/common/FileHelpersTest.cpp
169684 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file FileHelpersTest.cpp
15
/// @author Matthias Heppner
16
/// @author Michael Behrisch
17
/// @date 2009
18
///
19
// Tests FileHelpers class from <SUMO>/src/utils/common
20
/****************************************************************************/
21
22
// ===========================================================================
23
// included modules
24
// ===========================================================================
25
#include <config.h>
26
27
#include <gtest/gtest.h>
28
#include <utils/common/FileHelpers.h>
29
30
31
// ===========================================================================
32
// test definitions
33
// ===========================================================================
34
/* Tests the method checkForRelativity. Unify special filenames and make standard file paths relative to the config. */
35
TEST(FileHelpers, test_method_checkForRelativity) {
36
EXPECT_EQ("stdout", FileHelpers::checkForRelativity("-", "")) << "Special filename '-' should be treated as stdout";
37
EXPECT_EQ("/home/user/test.net.xml", FileHelpers::checkForRelativity("test.net.xml", "/home/user/test.sumocfg")) << "configuration path should be used.";
38
EXPECT_EQ("/test.net.xml", FileHelpers::checkForRelativity("test.net.xml", "/test.sumocfg")) << "configuration path should be used.";
39
}
40
41
42
TEST(FileHelpers, test_method_getConfigurationRelative) {
43
EXPECT_EQ("/home/user/test.net.xml", FileHelpers::getConfigurationRelative("/home/user/test.sumocfg", "test.net.xml")) << "configuration path should be used.";
44
EXPECT_EQ("/test.net.xml", FileHelpers::getConfigurationRelative("/test.sumocfg", "test.net.xml")) << "configuration path should be used.";
45
}
46
47
48
TEST(FileHelpers, test_method_getFilePath) {
49
EXPECT_EQ("/home/user/", FileHelpers::getFilePath("/home/user/test.sumocfg")) << "configuration path should be used.";
50
EXPECT_EQ("/", FileHelpers::getFilePath("/test.sumocfg")) << "configuration path should be used.";
51
}
52
53
54
TEST(FileHelpers, test_method_fixRelative) {
55
EXPECT_EQ("test.net.xml", FileHelpers::fixRelative("test.net.xml", "test.sumocfg", false, "/home/user")) << "no change for two filenames without paths.";
56
EXPECT_EQ("test.net.xml", FileHelpers::fixRelative("test.net.xml", "test.sumocfg", true, "/home/user")) << "no change for two filenames without paths.";
57
EXPECT_EQ("/home/user/test.net.xml", FileHelpers::fixRelative("/home/user/test.net.xml", "/home/user/test.sumocfg", false, "/home/user")) << "absolute path is kept without force.";
58
EXPECT_EQ("test.net.xml", FileHelpers::fixRelative("/home/user/test.net.xml", "/home/user/test.sumocfg", true, "/home/user")) << "absolute path is removed with force.";
59
EXPECT_EQ("test.net.xml", FileHelpers::fixRelative("user/test.net.xml", "user/test.sumocfg", false, "/home/user")) << "relative path is removed without force.";
60
EXPECT_EQ("test.net.xml", FileHelpers::fixRelative("user/test.net.xml", "user/test.sumocfg", true, "/home/user")) << "relative path is removed with force.";
61
EXPECT_EQ("subdir/test.net.xml", FileHelpers::fixRelative("subdir/test.net.xml", "test.sumocfg", false, "/home/user")) << "config without path means no change.";
62
EXPECT_EQ("subdir/test.net.xml", FileHelpers::fixRelative("subdir/test.net.xml", "test.sumocfg", true, "/home/user")) << "config without path means no change.";
63
EXPECT_EQ("user/subdir/test.net.xml", FileHelpers::fixRelative("subdir/test.net.xml", "../test.sumocfg", false, "/home/user")) << "config in parent dir makes path different.";
64
EXPECT_EQ("user/subdir/test.net.xml", FileHelpers::fixRelative("subdir/test.net.xml", "../test.sumocfg", true, "/home/user")) << "config in parent dir makes path different.";
65
EXPECT_EQ("/home/user/subdir/test.net.xml", FileHelpers::fixRelative("/home/user/subdir/test.net.xml", "../test.sumocfg", false, "/home/user")) << "config in parent dir does not change abs path without force.";
66
EXPECT_EQ("user/subdir/test.net.xml", FileHelpers::fixRelative("/home/user/subdir/test.net.xml", "../test.sumocfg", true, "/home/user")) << "config in parent dir makes path different.";
67
EXPECT_EQ("../../test.net.xml", FileHelpers::fixRelative("../test.net.xml", "subdir/test.sumocfg", false, "/home/user")) << "file in parent dir makes path different.";
68
EXPECT_EQ("../../test.net.xml", FileHelpers::fixRelative("../test.net.xml", "subdir/test.sumocfg", true, "/home/user")) << "file in parent dir makes path different.";
69
EXPECT_EQ("../../test.net.xml", FileHelpers::fixRelative("../test.net.xml", "/home/user/subdir/test.sumocfg", false, "/home/user")) << "file in parent dir makes path different.";
70
EXPECT_EQ("../../test.net.xml", FileHelpers::fixRelative("../test.net.xml", "/home/user/subdir/test.sumocfg", true, "/home/user")) << "file in parent dir makes path different.";
71
EXPECT_EQ("../../test.net.xml", FileHelpers::fixRelative("../../test.net.xml", "test.sumocfg", false, "/home/user")) << "file in parent dir with straight cfg should stay unchanged.";
72
EXPECT_EQ("../../test.net.xml", FileHelpers::fixRelative("../../test.net.xml", "test.sumocfg", true, "/home/user")) << "file in parent dir with straight cfg should stay unchanged.";
73
EXPECT_EQ("stdout", FileHelpers::fixRelative("stdout", "/home/sampledir", true, "/home")) << "check for reserved filenames.";
74
}
75
76