llvm/unittests/ADT/StringExtrasTest.cpp
Zachary Turner 3995e3e914 [StringExtras] Add a fromHex to complement toHex.
We already have a function toHex that will convert a string like
"\xFF\xFF" to the string "FFFF", but we do not have one that goes
the other way - i.e. to convert a textual string representing a
sequence of hexadecimal characters into the corresponding actual
bytes.  This patch adds such a function.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301356 91177308-0d34-0410-b5e6-96231b3b80d8
2017-04-25 20:21:35 +00:00

68 lines
2.2 KiB
C++

//===- StringExtrasTest.cpp - Unit tests for String extras ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringExtras.h"
#include "gtest/gtest.h"
using namespace llvm;
TEST(StringExtrasTest, Join) {
std::vector<std::string> Items;
EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> "));
Items = {"foo"};
EXPECT_EQ("foo", join(Items.begin(), Items.end(), " <sep> "));
Items = {"foo", "bar"};
EXPECT_EQ("foo <sep> bar", join(Items.begin(), Items.end(), " <sep> "));
Items = {"foo", "bar", "baz"};
EXPECT_EQ("foo <sep> bar <sep> baz",
join(Items.begin(), Items.end(), " <sep> "));
}
TEST(StringExtrasTest, JoinItems) {
const char *Foo = "foo";
std::string Bar = "bar";
llvm::StringRef Baz = "baz";
char X = 'x';
EXPECT_EQ("", join_items(" <sep> "));
EXPECT_EQ("", join_items('/'));
EXPECT_EQ("foo", join_items(" <sep> ", Foo));
EXPECT_EQ("foo", join_items('/', Foo));
EXPECT_EQ("foo <sep> bar", join_items(" <sep> ", Foo, Bar));
EXPECT_EQ("foo/bar", join_items('/', Foo, Bar));
EXPECT_EQ("foo <sep> bar <sep> baz", join_items(" <sep> ", Foo, Bar, Baz));
EXPECT_EQ("foo/bar/baz", join_items('/', Foo, Bar, Baz));
EXPECT_EQ("foo <sep> bar <sep> baz <sep> x",
join_items(" <sep> ", Foo, Bar, Baz, X));
EXPECT_EQ("foo/bar/baz/x", join_items('/', Foo, Bar, Baz, X));
}
TEST(StringExtrasTest, ToAndFromHex) {
std::vector<uint8_t> OddBytes = {0x5, 0xBD, 0x0D, 0x3E, 0xCD};
std::string OddStr = "05BD0D3ECD";
StringRef OddData(reinterpret_cast<const char *>(OddBytes.data()),
OddBytes.size());
EXPECT_EQ(OddStr, toHex(OddData));
EXPECT_EQ(OddData, fromHex(StringRef(OddStr).drop_front()));
std::vector<uint8_t> EvenBytes = {0xA5, 0xBD, 0x0D, 0x3E, 0xCD};
std::string EvenStr = "A5BD0D3ECD";
StringRef EvenData(reinterpret_cast<const char *>(EvenBytes.data()),
EvenBytes.size());
EXPECT_EQ(EvenStr, toHex(EvenData));
EXPECT_EQ(EvenData, fromHex(EvenStr));
}