blob: ffb4d9fe465a136272da19fed3ce063c1ac7d2df [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2014-2018 FIRST. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "frc/Preferences.h" // NOLINT(build/include_order)
9
10#include <cstdio>
11#include <fstream>
12
13#include <networktables/NetworkTableInstance.h>
14#include <ntcore.h>
15
16#include "frc/Timer.h"
17#include "gtest/gtest.h"
18
19using namespace frc;
20
21static const char* kFileName = "networktables.ini";
22static const double kSaveTime = 1.2;
23
24/**
25 * If we write a new networktables.ini with some sample values, test that
26 * we get those same values back using the Preference class.
27 */
28TEST(PreferencesTest, ReadPreferencesFromFile) {
29 auto inst = nt::NetworkTableInstance::GetDefault();
30 inst.StopServer();
31 std::remove(kFileName);
32 std::ofstream preferencesFile(kFileName);
33 preferencesFile << "[NetworkTables Storage 3.0]" << std::endl;
34 preferencesFile
35 << "string \"/Preferences/testFileGetString\"=\"Hello, preferences file\""
36 << std::endl;
37 preferencesFile << "double \"/Preferences/testFileGetInt\"=1" << std::endl;
38 preferencesFile << "double \"/Preferences/testFileGetDouble\"=0.5"
39 << std::endl;
40 preferencesFile << "double \"/Preferences/testFileGetFloat\"=0.25"
41 << std::endl;
42 preferencesFile << "boolean \"/Preferences/testFileGetBoolean\"=true"
43 << std::endl;
44 preferencesFile
45 << "double \"/Preferences/testFileGetLong\"=1000000000000000000"
46 << std::endl;
47 preferencesFile.close();
48 inst.StartServer();
49
50 Preferences* preferences = Preferences::GetInstance();
51 EXPECT_EQ("Hello, preferences file",
52 preferences->GetString("testFileGetString"));
53 EXPECT_EQ(1, preferences->GetInt("testFileGetInt"));
54 EXPECT_FLOAT_EQ(0.5, preferences->GetDouble("testFileGetDouble"));
55 EXPECT_FLOAT_EQ(0.25f, preferences->GetFloat("testFileGetFloat"));
56 EXPECT_TRUE(preferences->GetBoolean("testFileGetBoolean"));
57 EXPECT_EQ(1000000000000000000ll, preferences->GetLong("testFileGetLong"));
58}
59
60/**
61 * If we set some values using the Preferences class, test that they show up
62 * in networktables.ini
63 */
64TEST(PreferencesTest, WritePreferencesToFile) {
65 auto inst = nt::NetworkTableInstance::GetDefault();
66 inst.StartServer();
67 Preferences* preferences = Preferences::GetInstance();
68 preferences->Remove("testFileGetString");
69 preferences->Remove("testFileGetInt");
70 preferences->Remove("testFileGetDouble");
71 preferences->Remove("testFileGetFloat");
72 preferences->Remove("testFileGetBoolean");
73 preferences->Remove("testFileGetLong");
74
75 Wait(kSaveTime);
76
77 preferences->PutString("testFilePutString", "Hello, preferences file");
78 preferences->PutInt("testFilePutInt", 1);
79 preferences->PutDouble("testFilePutDouble", 0.5);
80 preferences->PutFloat("testFilePutFloat", 0.25f);
81 preferences->PutBoolean("testFilePutBoolean", true);
82 preferences->PutLong("testFilePutLong", 1000000000000000000ll);
83
84 Wait(kSaveTime);
85
86 static char const* kExpectedFileContents[] = {
87 "[NetworkTables Storage 3.0]",
88 "string \"/Preferences/.type\"=\"RobotPreferences\"",
89 "boolean \"/Preferences/testFilePutBoolean\"=true",
90 "double \"/Preferences/testFilePutDouble\"=0.5",
91 "double \"/Preferences/testFilePutFloat\"=0.25",
92 "double \"/Preferences/testFilePutInt\"=1",
93 "double \"/Preferences/testFilePutLong\"=1e+18",
94 "string \"/Preferences/testFilePutString\"=\"Hello, preferences file\""};
95
96 std::ifstream preferencesFile(kFileName);
97 for (auto& kExpectedFileContent : kExpectedFileContents) {
98 ASSERT_FALSE(preferencesFile.eof())
99 << "Preferences file prematurely reached EOF";
100
101 std::string line;
102 std::getline(preferencesFile, line);
103
104 ASSERT_EQ(kExpectedFileContent, line)
105 << "A line in networktables.ini was not correct";
106 }
107}