roslz4_test.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2014, Ben Charrow
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of Willow Garage, Inc. nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 ********************************************************************/
00034 
00035 #include <gtest/gtest.h>
00036 
00037 #include <roslz4/lz4s.h>
00038 
00039 class CompressATest :public testing::Test {
00040 protected:
00041   void SetUp() {
00042     for (size_t i = 0; i < sizeof(input); ++i) {
00043       input[i] = 'a';
00044     }
00045     for (size_t i = 0; i < sizeof(output); ++i) {
00046       output[i] = 0;
00047     }
00048     for (size_t i = 0; i < sizeof(other); ++i) {
00049       other[i] = 0;
00050     }
00051   }
00052 
00053   char input[1024];
00054   char output[1048];
00055   char other[1024];
00056 };
00057 
00058 TEST_F(CompressATest, Stream) {
00059   // Compression
00060   roslz4_stream stream;
00061   int ret;
00062   ret = roslz4_compressStart(&stream, 4);
00063   ASSERT_EQ(ROSLZ4_OK, ret);
00064 
00065   stream.input_left = sizeof(input);
00066   stream.input_next = input;
00067   stream.output_left = sizeof(output);
00068   stream.output_next = output;
00069 
00070   int counter;
00071   for (counter = 0; ret == ROSLZ4_OK; ++counter) {
00072     ret = roslz4_compress(&stream, ROSLZ4_FINISH);
00073   }
00074   ASSERT_EQ(ROSLZ4_STREAM_END, ret);
00075 
00076   int output_size = stream.output_next - output;
00077   roslz4_compressEnd(&stream);
00078 
00079   // Decompression
00080   stream.input_left = output_size;
00081   stream.input_next = output;
00082   stream.output_left = sizeof(other);
00083   stream.output_next = other;
00084 
00085   ret = roslz4_decompressStart(&stream);
00086   ASSERT_EQ(ROSLZ4_OK, ret);
00087 
00088   ret = roslz4_decompress(&stream);
00089   ASSERT_EQ(ROSLZ4_STREAM_END, ret);
00090 
00091   roslz4_decompressEnd(&stream);
00092 
00093   for (size_t i = 0; i < sizeof(other); ++i) {
00094     ASSERT_EQ(input[i], other[i]) <<  "Original and uncompressed data differ at index " << i;
00095   }
00096 }
00097 
00098 TEST_F(CompressATest, Oneshot) {
00099   // Compression
00100   unsigned int comp_size = sizeof(output);
00101   int ret = roslz4_buffToBuffCompress(input, sizeof(input), output, &comp_size,
00102                                       4);
00103   ASSERT_EQ(ROSLZ4_OK, ret);
00104 
00105   // Decompression
00106   unsigned int decomp_size = sizeof(other);
00107   ret = roslz4_buffToBuffDecompress(output, comp_size, other, &decomp_size);
00108   ASSERT_EQ(ROSLZ4_OK, ret);
00109   ASSERT_EQ(sizeof(input), decomp_size);
00110 
00111   for (size_t i = 0; i < sizeof(other); ++i) {
00112     ASSERT_EQ(input[i], other[i]) << "Original and uncompressed data differ at index " << i;
00113   }
00114 }
00115 
00116 TEST_F(CompressATest, OneshotDataCorruption) {
00117   unsigned int comp_size = sizeof(output);
00118   int ret = roslz4_buffToBuffCompress(input, sizeof(input), output, &comp_size,
00119                                       4);
00120   ASSERT_EQ(ROSLZ4_OK, ret);
00121 
00122   output[20] += 1;
00123 
00124   unsigned int decomp_size = sizeof(other);
00125   ret = roslz4_buffToBuffDecompress(output, comp_size, other, &decomp_size);
00126   ASSERT_EQ(ROSLZ4_DATA_ERROR, ret);
00127 }
00128 
00129 
00130 int main(int argc, char **argv) {
00131   testing::InitGoogleTest(&argc, argv);
00132   return RUN_ALL_TESTS();
00133 }


roslz4
Author(s): Ben Charrow
autogenerated on Thu Jun 6 2019 21:09:51