CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
106 lines
3.3 KiB
C++
106 lines
3.3 KiB
C++
//===- llvm/unittest/Support/ReverseIterationTest.cpp ---------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===---------------------------------------------------------------------===//
|
|
//
|
|
// Reverse Iteration unit tests.
|
|
//
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Support/ReverseIteration.h"
|
|
#include "llvm/ADT/DenseMap.h"
|
|
#include "llvm/ADT/DenseMapInfo.h"
|
|
#include "llvm/ADT/STLExtras.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
using namespace llvm;
|
|
|
|
TEST(ReverseIterationTest, DenseMapTest1) {
|
|
static_assert(detail::IsPointerLike<int *>::value,
|
|
"int * is pointer-like");
|
|
static_assert(detail::IsPointerLike<uintptr_t>::value,
|
|
"uintptr_t is pointer-like");
|
|
static_assert(!detail::IsPointerLike<int>::value,
|
|
"int is not pointer-like");
|
|
static_assert(detail::IsPointerLike<void *>::value,
|
|
"void * is pointer-like");
|
|
struct IncompleteType;
|
|
static_assert(detail::IsPointerLike<IncompleteType *>::value,
|
|
"incomplete * is pointer-like");
|
|
|
|
// For a DenseMap with non-pointer-like keys, forward iteration equals
|
|
// reverse iteration.
|
|
DenseMap<int, int> Map;
|
|
int Keys[] = { 1, 2, 3, 4 };
|
|
|
|
// Insert keys into the DenseMap.
|
|
for (auto Key: Keys)
|
|
Map[Key] = 0;
|
|
|
|
// Note: This is the observed order of keys in the DenseMap.
|
|
// If there is any change in the behavior of the DenseMap, this order
|
|
// would need to be adjusted accordingly.
|
|
int IterKeys[] = { 2, 4, 1, 3 };
|
|
|
|
// Check that the DenseMap is iterated in the expected order.
|
|
for (auto Tuple : zip(Map, IterKeys))
|
|
ASSERT_EQ(std::get<0>(Tuple).first, std::get<1>(Tuple));
|
|
|
|
// Check operator++ (post-increment).
|
|
int i = 0;
|
|
for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i)
|
|
ASSERT_EQ(iter->first, IterKeys[i]);
|
|
}
|
|
|
|
// Define a pointer-like int.
|
|
struct PtrLikeInt { int value; };
|
|
|
|
namespace llvm {
|
|
|
|
template<> struct DenseMapInfo<PtrLikeInt *> {
|
|
static PtrLikeInt *getEmptyKey() {
|
|
static PtrLikeInt EmptyKey;
|
|
return &EmptyKey;
|
|
}
|
|
|
|
static int getHashValue(const PtrLikeInt *P) {
|
|
return P->value;
|
|
}
|
|
|
|
static bool isEqual(const PtrLikeInt *LHS, const PtrLikeInt *RHS) {
|
|
return LHS == RHS;
|
|
}
|
|
};
|
|
|
|
} // end namespace llvm
|
|
|
|
TEST(ReverseIterationTest, DenseMapTest2) {
|
|
static_assert(detail::IsPointerLike<PtrLikeInt *>::value,
|
|
"PtrLikeInt * is pointer-like");
|
|
|
|
PtrLikeInt a = {4}, b = {8}, c = {12}, d = {16};
|
|
PtrLikeInt *Keys[] = { &a, &b, &c, &d };
|
|
|
|
// Insert keys into the DenseMap.
|
|
DenseMap<PtrLikeInt *, int> Map;
|
|
for (auto *Key : Keys)
|
|
Map[Key] = Key->value;
|
|
|
|
// Note: If there is any change in the behavior of the DenseMap,
|
|
// the observed order of keys would need to be adjusted accordingly.
|
|
if (shouldReverseIterate<PtrLikeInt *>())
|
|
std::reverse(&Keys[0], &Keys[4]);
|
|
|
|
// Check that the DenseMap is iterated in the expected order.
|
|
for (auto Tuple : zip(Map, Keys))
|
|
ASSERT_EQ(std::get<0>(Tuple).second, std::get<1>(Tuple)->value);
|
|
|
|
// Check operator++ (post-increment).
|
|
int i = 0;
|
|
for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i)
|
|
ASSERT_EQ(iter->second, Keys[i]->value);
|
|
}
|