Turi Create  4.0
testing_utils.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_GENERAL_TESTING_UTILS_H_
7 #define TURI_GENERAL_TESTING_UTILS_H_
8 
9 #include <core/parallel/pthread_tools.hpp>
10 #include <core/util/cityhash_tc.hpp>
11 #include <core/util/try_finally.hpp>
12 #include <core/random/random.hpp>
13 #include <core/storage/serialization/serialization_includes.hpp>
14 #include <vector>
15 #include <string>
16 #include <locale>
17 
18 #include <core/parallel/mutex.hpp>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <boost/filesystem.hpp>
22 
23 namespace turi {
24 
25 /** The directories we use for our temporary archives should be unique
26  * and everything, but we don't want hundreds of these lying around.
27  * Thus add them to a list with which we delete when the program
28  * exits; this function does that.
29  */
30 void _add_directory_to_deleter(const std::string& name);
31 
32 /** Make a unique directory name.
33  */
34 std::string _get_unique_directory(const std::string& file, size_t line);
35 
36 
37 /**
38  * \ingroup util
39  * Serializes and deserializes a model, making sure that the
40  * model leaves the stream iterator in the appropriate place.
41  */
42 template <typename T, typename U>
43 void _save_and_load_object(T& dest, const U& src, std::string dir) {
44 
45  // Create the directory
48 
49  std::string arc_name = dir + "/test_archive";
50 
51  uint64_t random_number = hash64(random::fast_uniform<size_t>(0,size_t(-1)));
52 
53  // Save it
54  dir_archive archive_write;
55  archive_write.open_directory_for_write(arc_name);
56 
57  turi::oarchive oarc(archive_write);
58 
59  oarc << src << random_number;
60 
61  archive_write.close();
62 
63  // Load it
64  dir_archive archive_read;
65  archive_read.open_directory_for_read(arc_name);
66 
67  turi::iarchive iarc(archive_read);
68 
69  iarc >> dest;
70 
71  uint64_t test_number;
72 
73  iarc >> test_number;
74 
75  archive_read.close();
76 
77  ASSERT_EQ(test_number, random_number);
78 }
79 
80 #define save_and_load_object(dest, src) \
81  do{ \
82  _save_and_load_object( \
83  dest, src, _get_unique_directory(__FILE__, __LINE__)); \
84  } while(false)
85 
86 }
87 
88 #endif /* _TESTING_UTILS_H_ */
void open_directory_for_write(std::string directory, bool fail_on_existing=false)
void open_directory_for_read(std::string directory)
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
Definition: iarchive.hpp:60
bool create_directory(const std::string &path)
void _add_directory_to_deleter(const std::string &name)
static uint64_t hash64(const char *s, size_t len)
void _save_and_load_object(T &dest, const U &src, std::string dir)
std::string _get_unique_directory(const std::string &file, size_t line)
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
Definition: oarchive.hpp:80