1 : // Copyright 2012 Google Inc.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declares some unittest helper functions.
16 :
17 : #ifndef SYZYGY_CORE_UNITTEST_UTIL_H_
18 : #define SYZYGY_CORE_UNITTEST_UTIL_H_
19 :
20 : #include "base/file_path.h"
21 : #include "syzygy/core/serialization.h"
22 :
23 : namespace testing {
24 :
25 : // This defines a simple test of serialization for a given object. Returns
26 : // true on success, false otherwise. The data object must be default
27 : // constructible and comparable.
28 E : template<class Data> bool TestSerialization(const Data& data) {
29 E : core::ByteVector bytes;
30 :
31 E : core::ScopedOutStreamPtr out_stream;
32 E : out_stream.reset(core::CreateByteOutStream(std::back_inserter(bytes)));
33 E : core::NativeBinaryOutArchive out_archive(out_stream.get());
34 E : if (!out_archive.Save(data))
35 i : return false;
36 E : if (!out_archive.Flush())
37 i : return false;
38 :
39 E : core::ScopedInStreamPtr in_stream;
40 E : in_stream.reset(core::CreateByteInStream(bytes.begin(), bytes.end()));
41 E : core::NativeBinaryInArchive in_archive(in_stream.get());
42 E : Data data_copy;
43 E : if (!in_archive.Load(&data_copy))
44 i : return false;
45 :
46 : // Ensure the two elements are the same after a roundtrip through the
47 : // serialization engine.
48 E : return (data == data_copy);
49 E : };
50 :
51 : // Same as above, but serializes to the given file, which has to be opened
52 : // in read-write mode.
53 E : template<class Data> bool TestSerialization(const Data& data, FILE* file) {
54 E : core::FileOutStream out_stream(file);
55 E : core::NativeBinaryOutArchive out_archive(&out_stream);
56 E : if (!out_archive.Save(data))
57 i : return false;
58 :
59 : // Flush the output and rewind the file.
60 E : fflush(file);
61 E : fseek(file, 0, SEEK_SET);
62 :
63 E : core::FileInStream in_stream(file);
64 E : core::NativeBinaryInArchive in_archive(&in_stream);
65 E : Data data_copy;
66 E : if (!in_archive.Load(&data_copy))
67 i : return false;
68 :
69 : // Ensure the two elements are the same after a roundtrip through the
70 : // serialization engine.
71 E : return (data == data_copy);
72 E : };
73 :
74 : // Converts a relative path to absolute using the src directory as base.
75 : //
76 : // @path rel_path the relative path to convert.
77 : // @returns an absolute path.
78 : FilePath GetSrcRelativePath(const wchar_t* rel_path);
79 :
80 : // Converts a relative path to absolute using the executable directory as base.
81 : //
82 : // @path rel_path the relative path to convert.
83 : // @returns an absolute path.
84 : FilePath GetExeRelativePath(const wchar_t* rel_path);
85 :
86 : // Converts a relative path to absolute using the output directory as base.
87 : //
88 : // @path rel_path the relative path to convert.
89 : // @returns an absolute path.
90 : FilePath GetOutputRelativePath(const wchar_t* rel_path);
91 :
92 : // Converts a relative path to absolute using the test_data directory as base.
93 : //
94 : // @path rel_path the relative path to convert.
95 : // @returns an absolute path.
96 : FilePath GetExeTestDataRelativePath(const wchar_t* rel_path);
97 :
98 : // Converts an absolute path to a relative path using the given root directory
99 : // as a base.
100 : //
101 : // @param abs_path the absolute path to convert.
102 : // @param root_path the root path to use.
103 : // @returns the relative path to abs_path, starting from root. If there is no
104 : // relative path, it returns the empty path.
105 : // @pre Both abs_path and root_path must be absolute paths.
106 : FilePath GetRelativePath(const FilePath& abs_path, const FilePath& root_path);
107 :
108 : // Converts an absolute path to a relative path using the current working
109 : // directory as a base.
110 : //
111 : // @param abs_path the absolute path to convert.
112 : // @returns the relative path to abs_path, starting from the current workign
113 : // directory. If there is no relative path, it returns the empty path.
114 : FilePath GetRelativePath(const FilePath& abs_path);
115 :
116 : } // namespace testing
117 :
118 : #endif // SYZYGY_CORE_UNITTEST_UTIL_H_
|