diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-08-23 20:31:39 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-08-23 20:31:39 +0000 |
commit | 9441cfe4880018a85af9710ad405d4f2e522acc2 (patch) | |
tree | ce52791943346ccf557a8aa28979b3f157d58f4e /unittests/Support | |
parent | a235d13217ff14621a88f3ea96a8a3b980c56d02 (diff) | |
download | external_llvm-9441cfe4880018a85af9710ad405d4f2e522acc2.zip external_llvm-9441cfe4880018a85af9710ad405d4f2e522acc2.tar.gz external_llvm-9441cfe4880018a85af9710ad405d4f2e522acc2.tar.bz2 |
Fix off-by-one in llvm::Format::print.
- This also shortens the Format.h implementation, and uses the print buffer
fully (it was wasting a character).
- This manifested as llvm-test failures, because one side effect was that
raw_ostream would write garbage '\x00' values into the output stream if it
happened that the string was at the end of the buffer. This meant that grep
would report 'Binary file matches', which meant the silly pattern matching
llvm-test eventually does would fail. Cute. :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79862 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Support')
-rw-r--r-- | unittests/Support/raw_ostream_test.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/unittests/Support/raw_ostream_test.cpp b/unittests/Support/raw_ostream_test.cpp index 3e22a05..52639ba 100644 --- a/unittests/Support/raw_ostream_test.cpp +++ b/unittests/Support/raw_ostream_test.cpp @@ -8,6 +8,8 @@ //===----------------------------------------------------------------------===// #include "gtest/gtest.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -20,6 +22,23 @@ template<typename T> std::string printToString(const T &Value) { return res; } +/// printToString - Print the given value to a stream which only has \arg +/// BytesLeftInBuffer bytes left in the buffer. This is useful for testing edge +/// cases in the buffer handling logic. +template<typename T> std::string printToString(const T &Value, + unsigned BytesLeftInBuffer) { + // FIXME: This is relying on internal knowledge of how raw_ostream works to + // get the buffer position right. + SmallString<256> SVec; + assert(BytesLeftInBuffer < 256 && "Invalid buffer count!"); + llvm::raw_svector_ostream OS(SVec); + unsigned StartIndex = 256 - BytesLeftInBuffer; + for (unsigned i = 0; i != StartIndex; ++i) + OS << '?'; + OS << Value; + return OS.str().substr(StartIndex); +} + template<typename T> std::string printToStringUnbuffered(const T &Value) { std::string res; llvm::raw_string_ostream OS(res); @@ -90,4 +109,12 @@ TEST(raw_ostreamTest, Types_Unbuffered) { EXPECT_EQ("-9223372036854775808", printToStringUnbuffered(INT64_MIN)); } +TEST(raw_ostreamTest, BufferEdge) { + EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 1)); + EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 2)); + EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 3)); + EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 4)); + EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 10)); +} + } |