Ocean
TestQRCodeEncoder.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
11 
14 
17 
18 #ifdef OCEAN_USE_LOCAL_TEST_DATA_COLLECTION
19  #ifndef OCEAN_USE_TEST_DATA_COLLECTION
20  #define OCEAN_USE_TEST_DATA_COLLECTION
21  #endif
22 #endif // OCEAN_USE_LOCAL_TEST_DATA_COLLECTION
23 
24 namespace Ocean
25 {
26 
27 namespace Test
28 {
29 
30 namespace TestCV
31 {
32 
33 namespace TestDetector
34 {
35 
36 namespace TestQRCodes
37 {
38 
39 /**
40  * This class implements tests for the QR code features
41  * @ingroup testcvdetectorqrcodes
42  */
43 class OCEAN_TEST_CV_DETECTOR_QRCODES_EXPORT TestQRCodeEncoder
44 {
45  friend class TestQRCodeDecoder;
46 
47  public:
48 
49  /**
50  * This class implements a test data collection based on a local file.
51  */
53  {
54  public:
55 
56  /**
57  * Creates a new test data collection object.
58  * @param filename The filename of the file containing the test data
59  */
60  explicit FileDataCollection(const std::string& filename);
61 
62  /**
63  * Returns the test data object associated with a specified index.
64  * @see TestDataCollection::data().
65  */
66  SharedTestData data(const size_t index) override;
67 
68  /**
69  * Returns the number of data object objects this collection holds.
70  * @see TestDataCollection::size().
71  */
72  size_t size() override;
73 
74  protected:
75 
76  /// The filename of the test data belonging to this test collection.
77  std::string filename_;
78  };
79 
80  protected:
81 
82  /**
83  * Definition of a helper data structure that's used to verify the QR code encoding functionality
84  * This will contain the raw message and the values of corresponding QR code that were generated with a reference QR code generator and which are assumed to be correct.
85  */
87  {
88  public:
89 
90  /**
91  * Constructor - creates an invalid instance
92  */
94 
95  /**
96  * Constructor
97  * @param version The version of the QR code
98  * @param errorCorrectionCapacity The level of error correction that the QR code has
99  * @param maskingPattern The masking pattern that was used to generate the QR code
100  * @param message The raw input message that was used to generate the QR code
101  * @param modules The modules of a QR code as a string ("1", "0" only), must have `(4 * version + 17)^2` elements
102  * @sa loadCSVTestQRCodeEncoding()
103  */
104  inline QRCodeVerificationItem(const unsigned int version, const CV::Detector::QRCodes::QRCode::ErrorCorrectionCapacity errorCorrectionCapacity, const CV::Detector::QRCodes::QRCodeEncoder::MaskingPattern maskingPattern, const std::string& message, const std::string& modules);
105 
106  public:
107 
108  /// Version number
109  unsigned int version_ = 0u;
110 
111  /// Error correction capacity
113 
114  /// Masking pattern
116 
117  /// The raw message
118  std::string message_;
119 
120  /// The modules (bit matrix)
121  std::string modules_;
122  };
123 
124  /// Vector of verification items
125  typedef std::vector<QRCodeVerificationItem> QRCodeVerificationItems;
126 
127  public:
128 
129  /**
130  * Tests the QR code functions.
131  * @param testDuration Number of seconds for each test, with range (0, infinity)
132  * @return True, if succeeded
133  */
134  static bool test(const double testDuration);
135 
136  /**
137  * Tests encoding (generation) of QR codes
138  * @param testDuration The duration in seconds for which this test will be run, must be > 0.0
139  * @return True, if succeeded
140  */
141  static bool testQRCodeEncoding(const double testDuration);
142 
143  /**
144  * Tests the encoding/decoding of the version information
145  * @return True, if succeeded
146  */
148 
149  /**
150  * Tests the encoding/decoding of the format information
151  * @return True, if succeeded
152  */
154 
155  protected:
156 
157  /**
158  * Provides verification data for the QR code encoding test.
159  * The source is either a test data collection, or a minimal dataset
160  * @return The verification data, empty if the data could not be loaded
161  */
163 
164  /**
165  * Loads the verification data for the QR code encoding test from a buffer containing a CSV file.
166  * @param buffer The pointer to the buffer, must be valid
167  * @param size The size of the buffer, in bytes, with range [1, infinity)
168  * @return The verification data
169  */
170  static QRCodeVerificationItems loadCSVTestQRCodeEncoding(const void* buffer, const size_t size);
171 
172  /**
173  * Converts a line from a CSV file into a helper data structure that is subsequently used for testing
174  * @param lineCSV A line for a CSV file, cf. `loadDataTestQRCodeEncoding()`
175  * @param qrcodeVerificationItem The converted line from a CSV that will be stored here
176  * @return True on success, otherwise false
177  */
178  static bool convertCSVToQRCodeVerificationItem(const std::string& lineCSV, QRCodeVerificationItem& qrcodeVerificationItem);
179 };
180 
181 #ifdef OCEAN_USE_TEST_DATA_COLLECTION
182 
183 /**
184  * Registers the data collections for the QRCodeEncoder test.
185  * @return The scoped subscription for the registered data collection
186  */
188 
189 #endif // OCEAN_USE_LOCAL_TEST_DATA_COLLECTION
190 
191 inline TestQRCodeEncoder::QRCodeVerificationItem::QRCodeVerificationItem(const unsigned int version, const CV::Detector::QRCodes::QRCode::ErrorCorrectionCapacity errorCorrectionCapacity, const CV::Detector::QRCodes::QRCodeEncoder::MaskingPattern maskingPattern, const std::string& message, const std::string& modules) :
192  version_(version),
193  errorCorrectionCapacity_(errorCorrectionCapacity),
194  maskingPattern_(maskingPattern),
195  message_(message),
196  modules_(modules)
197 {
198  // Nothing else to do.
199 }
200 
201 } // namespace TestQRCodes
202 
203 } // namespace TestDetector
204 
205 } // namespace TestCV
206 
207 } // namespace Test
208 
209 } // namespace Ocean
ErrorCorrectionCapacity
Enumeration of the levels of error correction The value of the enums correspond to the standard-defin...
Definition: QRCodeBase.h:53
@ ECC_INVALID
Indicator for an invalid error correction capacity.
Definition: QRCodeBase.h:65
MaskingPattern
Enum for the mask patterns used to shuffle modules of a QR code.
Definition: QRCodeEncoder.h:45
@ MP_PATTERN_UNKNOWN
Denotes unknown masking patterns (not part of the standard)
Definition: QRCodeEncoder.h:71
This class implements a subscription object which can be used unique subscriptions to e....
Definition: ScopedSubscription.h:28
This class implements tests for the QR code features.
Definition: TestQRCodeDecoder.h:35
This class implements a test data collection based on a local file.
Definition: TestQRCodeEncoder.h:53
FileDataCollection(const std::string &filename)
Creates a new test data collection object.
SharedTestData data(const size_t index) override
Returns the test data object associated with a specified index.
size_t size() override
Returns the number of data object objects this collection holds.
std::string filename_
The filename of the test data belonging to this test collection.
Definition: TestQRCodeEncoder.h:77
Definition of a helper data structure that's used to verify the QR code encoding functionality This w...
Definition: TestQRCodeEncoder.h:87
std::string message_
The raw message.
Definition: TestQRCodeEncoder.h:118
std::string modules_
The modules (bit matrix)
Definition: TestQRCodeEncoder.h:121
This class implements tests for the QR code features.
Definition: TestQRCodeEncoder.h:44
static bool testQRCodeFormatEncodingDecoding()
Tests the encoding/decoding of the format information.
static bool convertCSVToQRCodeVerificationItem(const std::string &lineCSV, QRCodeVerificationItem &qrcodeVerificationItem)
Converts a line from a CSV file into a helper data structure that is subsequently used for testing.
static QRCodeVerificationItems loadDataTestQRCodeEncoding()
Provides verification data for the QR code encoding test.
std::vector< QRCodeVerificationItem > QRCodeVerificationItems
Vector of verification items.
Definition: TestQRCodeEncoder.h:125
static bool test(const double testDuration)
Tests the QR code functions.
static bool testQRCodeEncoding(const double testDuration)
Tests encoding (generation) of QR codes.
static QRCodeVerificationItems loadCSVTestQRCodeEncoding(const void *buffer, const size_t size)
Loads the verification data for the QR code encoding test from a buffer containing a CSV file.
static bool testQRCodeVersionEncodingDecoding()
Tests the encoding/decoding of the version information.
This class is the base class for all TestDataCollection objects.
Definition: TestDataCollection.h:35
std::shared_ptr< TestData > SharedTestData
Definition of a shared pointer holding a TestData object.
Definition: TestData.h:29
TestDataManager::ScopedSubscription TestQRCodeEncoder_registerTestDataCollection()
Registers the data collections for the QRCodeEncoder test.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15