| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // http://code.google.com/p/protobuf/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 25 matching lines...) Expand all Loading... |
| 39 // of varying sizes, with a BackUp() after each chunk. It is read back | 39 // of varying sizes, with a BackUp() after each chunk. It is read back |
| 40 // similarly, but with chunks separated at different points. The whole | 40 // similarly, but with chunks separated at different points. The whole |
| 41 // process is run with a variety of block sizes for both the input and | 41 // process is run with a variety of block sizes for both the input and |
| 42 // the output. | 42 // the output. |
| 43 // | 43 // |
| 44 // TODO(kenton): Rewrite this test to bring it up to the standards of all | 44 // TODO(kenton): Rewrite this test to bring it up to the standards of all |
| 45 // the other proto2 tests. May want to wait for gTest to implement | 45 // the other proto2 tests. May want to wait for gTest to implement |
| 46 // "parametized tests" so that one set of tests can be used on all the | 46 // "parametized tests" so that one set of tests can be used on all the |
| 47 // implementations. | 47 // implementations. |
| 48 | 48 |
| 49 #include "config.h" |
| 50 |
| 49 #ifdef _MSC_VER | 51 #ifdef _MSC_VER |
| 50 #include <io.h> | 52 #include <io.h> |
| 51 #else | 53 #else |
| 52 #include <unistd.h> | 54 #include <unistd.h> |
| 53 #endif | 55 #endif |
| 54 #include <stdlib.h> | 56 #include <stdlib.h> |
| 55 #include <sys/types.h> | 57 #include <sys/types.h> |
| 56 #include <sys/stat.h> | 58 #include <sys/stat.h> |
| 57 #include <fcntl.h> | 59 #include <fcntl.h> |
| 58 #include <errno.h> | 60 #include <errno.h> |
| (...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 result.append(reinterpret_cast<const char*>(buffer), size); | 553 result.append(reinterpret_cast<const char*>(buffer), size); |
| 552 } | 554 } |
| 553 } | 555 } |
| 554 return result; | 556 return result; |
| 555 } | 557 } |
| 556 | 558 |
| 557 TEST_F(IoTest, CompressionOptions) { | 559 TEST_F(IoTest, CompressionOptions) { |
| 558 // Some ad-hoc testing of compression options. | 560 // Some ad-hoc testing of compression options. |
| 559 | 561 |
| 560 string golden; | 562 string golden; |
| 561 GOOGLE_CHECK_OK(File::GetContents( | 563 File::ReadFileToStringOrDie( |
| 562 TestSourceDir() + | 564 TestSourceDir() + "/google/protobuf/testdata/golden_message", |
| 563 "/google/protobuf/testdata/golden_message", | 565 &golden); |
| 564 &golden, true)); | |
| 565 | 566 |
| 566 GzipOutputStream::Options options; | 567 GzipOutputStream::Options options; |
| 567 string gzip_compressed = Compress(golden, options); | 568 string gzip_compressed = Compress(golden, options); |
| 568 | 569 |
| 569 options.compression_level = 0; | 570 options.compression_level = 0; |
| 570 string not_compressed = Compress(golden, options); | 571 string not_compressed = Compress(golden, options); |
| 571 | 572 |
| 572 // Try zlib compression for fun. | 573 // Try zlib compression for fun. |
| 573 options = GzipOutputStream::Options(); | 574 options = GzipOutputStream::Options(); |
| 574 options.format = GzipOutputStream::ZLIB; | 575 options.format = GzipOutputStream::ZLIB; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 | 648 |
| 648 delete coded_input; | 649 delete coded_input; |
| 649 delete gzin; | 650 delete gzin; |
| 650 delete input; | 651 delete input; |
| 651 } | 652 } |
| 652 } | 653 } |
| 653 | 654 |
| 654 delete [] temp_buffer; | 655 delete [] temp_buffer; |
| 655 delete [] buffer; | 656 delete [] buffer; |
| 656 } | 657 } |
| 657 | |
| 658 TEST_F(IoTest, GzipInputByteCountAfterClosed) { | |
| 659 string golden = "abcdefghijklmnopqrstuvwxyz"; | |
| 660 string compressed = Compress(golden, GzipOutputStream::Options()); | |
| 661 | |
| 662 for (int i = 0; i < kBlockSizeCount; i++) { | |
| 663 ArrayInputStream arr_input(compressed.data(), compressed.size(), | |
| 664 kBlockSizes[i]); | |
| 665 GzipInputStream gz_input(&arr_input); | |
| 666 const void* buffer; | |
| 667 int size; | |
| 668 while (gz_input.Next(&buffer, &size)) { | |
| 669 EXPECT_LE(gz_input.ByteCount(), golden.size()); | |
| 670 } | |
| 671 EXPECT_EQ(golden.size(), gz_input.ByteCount()); | |
| 672 } | |
| 673 } | |
| 674 | |
| 675 TEST_F(IoTest, GzipInputByteCountAfterClosedConcatenatedStreams) { | |
| 676 string golden1 = "abcdefghijklmnopqrstuvwxyz"; | |
| 677 string golden2 = "the quick brown fox jumps over the lazy dog"; | |
| 678 const size_t total_size = golden1.size() + golden2.size(); | |
| 679 string compressed = Compress(golden1, GzipOutputStream::Options()) + | |
| 680 Compress(golden2, GzipOutputStream::Options()); | |
| 681 | |
| 682 for (int i = 0; i < kBlockSizeCount; i++) { | |
| 683 ArrayInputStream arr_input(compressed.data(), compressed.size(), | |
| 684 kBlockSizes[i]); | |
| 685 GzipInputStream gz_input(&arr_input); | |
| 686 const void* buffer; | |
| 687 int size; | |
| 688 while (gz_input.Next(&buffer, &size)) { | |
| 689 EXPECT_LE(gz_input.ByteCount(), total_size); | |
| 690 } | |
| 691 EXPECT_EQ(total_size, gz_input.ByteCount()); | |
| 692 } | |
| 693 } | |
| 694 #endif | 658 #endif |
| 695 | 659 |
| 696 // There is no string input, only string output. Also, it doesn't support | 660 // There is no string input, only string output. Also, it doesn't support |
| 697 // explicit block sizes. So, we'll only run one test and we'll use | 661 // explicit block sizes. So, we'll only run one test and we'll use |
| 698 // ArrayInput to read back the results. | 662 // ArrayInput to read back the results. |
| 699 TEST_F(IoTest, StringIo) { | 663 TEST_F(IoTest, StringIo) { |
| 700 string str; | 664 string str; |
| 701 { | 665 { |
| 702 StringOutputStream output(&str); | 666 StringOutputStream output(&str); |
| 703 WriteStuff(&output); | 667 WriteStuff(&output); |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 952 ArrayOutputStream output(buffer, kBufferSize); | 916 ArrayOutputStream output(buffer, kBufferSize); |
| 953 WriteStuff(&output); | 917 WriteStuff(&output); |
| 954 | 918 |
| 955 // Set up input. | 919 // Set up input. |
| 956 ArrayInputStream array_input(buffer, kBufferSize); | 920 ArrayInputStream array_input(buffer, kBufferSize); |
| 957 LimitingInputStream input(&array_input, output.ByteCount()); | 921 LimitingInputStream input(&array_input, output.ByteCount()); |
| 958 | 922 |
| 959 ReadStuff(&input); | 923 ReadStuff(&input); |
| 960 } | 924 } |
| 961 | 925 |
| 962 // Checks that ByteCount works correctly for LimitingInputStreams where the | |
| 963 // underlying stream has already been read. | |
| 964 TEST_F(IoTest, LimitingInputStreamByteCount) { | |
| 965 const int kHalfBufferSize = 128; | |
| 966 const int kBufferSize = kHalfBufferSize * 2; | |
| 967 uint8 buffer[kBufferSize]; | |
| 968 | |
| 969 // Set up input. Only allow half to be read at once. | |
| 970 ArrayInputStream array_input(buffer, kBufferSize, kHalfBufferSize); | |
| 971 const void* data; | |
| 972 int size; | |
| 973 EXPECT_TRUE(array_input.Next(&data, &size)); | |
| 974 EXPECT_EQ(kHalfBufferSize, array_input.ByteCount()); | |
| 975 // kHalfBufferSize - 1 to test limiting logic as well. | |
| 976 LimitingInputStream input(&array_input, kHalfBufferSize - 1); | |
| 977 EXPECT_EQ(0, input.ByteCount()); | |
| 978 EXPECT_TRUE(input.Next(&data, &size)); | |
| 979 EXPECT_EQ(kHalfBufferSize - 1 , input.ByteCount()); | |
| 980 } | |
| 981 | |
| 982 // Check that a zero-size array doesn't confuse the code. | 926 // Check that a zero-size array doesn't confuse the code. |
| 983 TEST(ZeroSizeArray, Input) { | 927 TEST(ZeroSizeArray, Input) { |
| 984 ArrayInputStream input(NULL, 0); | 928 ArrayInputStream input(NULL, 0); |
| 985 const void* data; | 929 const void* data; |
| 986 int size; | 930 int size; |
| 987 EXPECT_FALSE(input.Next(&data, &size)); | 931 EXPECT_FALSE(input.Next(&data, &size)); |
| 988 } | 932 } |
| 989 | 933 |
| 990 TEST(ZeroSizeArray, Output) { | 934 TEST(ZeroSizeArray, Output) { |
| 991 ArrayOutputStream output(NULL, 0); | 935 ArrayOutputStream output(NULL, 0); |
| 992 void* data; | 936 void* data; |
| 993 int size; | 937 int size; |
| 994 EXPECT_FALSE(output.Next(&data, &size)); | 938 EXPECT_FALSE(output.Next(&data, &size)); |
| 995 } | 939 } |
| 996 | 940 |
| 997 } // namespace | 941 } // namespace |
| 998 } // namespace io | 942 } // namespace io |
| 999 } // namespace protobuf | 943 } // namespace protobuf |
| 1000 } // namespace google | 944 } // namespace google |
| OLD | NEW |