roslz4_test.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2014, Ben Charrow
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 ********************************************************************/
34 
35 #include <gtest/gtest.h>
36 
37 #include <roslz4/lz4s.h>
38 
39 class CompressATest :public testing::Test {
40 protected:
41  void SetUp() {
42  for (size_t i = 0; i < sizeof(input); ++i) {
43  input[i] = 'a';
44  }
45  for (size_t i = 0; i < sizeof(output); ++i) {
46  output[i] = 0;
47  }
48  for (size_t i = 0; i < sizeof(other); ++i) {
49  other[i] = 0;
50  }
51  }
52 
53  char input[1024];
54  char output[1048];
55  char other[1024];
56 };
57 
59  // Compression
60  roslz4_stream stream;
61  int ret;
62  ret = roslz4_compressStart(&stream, 4);
63  ASSERT_EQ(ROSLZ4_OK, ret);
64 
65  stream.input_left = sizeof(input);
66  stream.input_next = input;
67  stream.output_left = sizeof(output);
68  stream.output_next = output;
69 
70  int counter;
71  for (counter = 0; ret == ROSLZ4_OK; ++counter) {
72  ret = roslz4_compress(&stream, ROSLZ4_FINISH);
73  }
74  ASSERT_EQ(ROSLZ4_STREAM_END, ret);
75 
76  int output_size = stream.output_next - output;
77  roslz4_compressEnd(&stream);
78 
79  // Decompression
80  stream.input_left = output_size;
81  stream.input_next = output;
82  stream.output_left = sizeof(other);
83  stream.output_next = other;
84 
85  ret = roslz4_decompressStart(&stream);
86  ASSERT_EQ(ROSLZ4_OK, ret);
87 
88  ret = roslz4_decompress(&stream);
89  ASSERT_EQ(ROSLZ4_STREAM_END, ret);
90 
91  roslz4_decompressEnd(&stream);
92 
93  for (size_t i = 0; i < sizeof(other); ++i) {
94  ASSERT_EQ(input[i], other[i]) << "Original and uncompressed data differ at index " << i;
95  }
96 }
97 
98 TEST_F(CompressATest, Oneshot) {
99  // Compression
100  unsigned int comp_size = sizeof(output);
101  int ret = roslz4_buffToBuffCompress(input, sizeof(input), output, &comp_size,
102  4);
103  ASSERT_EQ(ROSLZ4_OK, ret);
104 
105  // Decompression
106  unsigned int decomp_size = sizeof(other);
107  ret = roslz4_buffToBuffDecompress(output, comp_size, other, &decomp_size);
108  ASSERT_EQ(ROSLZ4_OK, ret);
109  ASSERT_EQ(sizeof(input), decomp_size);
110 
111  for (size_t i = 0; i < sizeof(other); ++i) {
112  ASSERT_EQ(input[i], other[i]) << "Original and uncompressed data differ at index " << i;
113  }
114 }
115 
116 TEST_F(CompressATest, OneshotDataCorruption) {
117  unsigned int comp_size = sizeof(output);
118  int ret = roslz4_buffToBuffCompress(input, sizeof(input), output, &comp_size,
119  4);
120  ASSERT_EQ(ROSLZ4_OK, ret);
121 
122  output[20] += 1;
123 
124  unsigned int decomp_size = sizeof(other);
125  ret = roslz4_buffToBuffDecompress(output, comp_size, other, &decomp_size);
126  ASSERT_EQ(ROSLZ4_DATA_ERROR, ret);
127 }
128 
129 
130 int main(int argc, char **argv) {
131  testing::InitGoogleTest(&argc, argv);
132  return RUN_ALL_TESTS();
133 }
void roslz4_compressEnd(roslz4_stream *stream)
Definition: lz4s.c:353
TEST_F(CompressATest, Stream)
Definition: roslz4_test.cpp:58
int roslz4_compress(roslz4_stream *stream, int action)
Definition: lz4s.c:317
int roslz4_decompressStart(roslz4_stream *stream)
Definition: lz4s.c:359
char other[1024]
Definition: roslz4_test.cpp:55
char input[1024]
Definition: roslz4_test.cpp:53
int roslz4_buffToBuffDecompress(char *input, unsigned int input_size, char *output, unsigned int *output_size)
Definition: lz4s.c:607
const int ROSLZ4_STREAM_END
Definition: lz4s.h:51
int input_left
Definition: lz4s.h:59
void roslz4_decompressEnd(roslz4_stream *str)
Definition: lz4s.c:567
char * output_next
Definition: lz4s.h:61
char * input_next
Definition: lz4s.h:58
const int ROSLZ4_DATA_ERROR
Definition: lz4s.h:47
const int ROSLZ4_OK
Definition: lz4s.h:50
int roslz4_buffToBuffCompress(char *input, unsigned int input_size, char *output, unsigned int *output_size, int block_size_id)
Definition: lz4s.c:573
int roslz4_compressStart(roslz4_stream *stream, int block_size_id)
Definition: lz4s.c:311
const int ROSLZ4_FINISH
Definition: lz4s.h:55
int roslz4_decompress(roslz4_stream *stream)
Definition: lz4s.c:536
char output[1048]
Definition: roslz4_test.cpp:54
int output_left
Definition: lz4s.h:62
int main(int argc, char **argv)


roslz4
Author(s): Ben Charrow
autogenerated on Mon Nov 2 2020 03:52:18