| 1 |
//===- PresburgerSpaceTest.cpp - Tests for PresburgerSpace ----------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "mlir/Analysis/Presburger/PresburgerSpace.h" |
| 10 |
#include <gmock/gmock.h> |
| 11 |
#include <gtest/gtest.h> |
| 12 |
|
| 13 |
using namespace mlir; |
| 14 |
using namespace presburger; |
| 15 |
|
| 16 |
TEST(PresburgerSpaceTest, insertId) { |
| 17 |
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 2, 1); |
| 18 |
|
| 19 |
// Try inserting 2 domain ids. |
| 20 |
space.insertVar(VarKind::Domain, 0, 2); |
| 21 |
EXPECT_EQ(space.getNumDomainVars(), 4u); |
| 22 |
|
| 23 |
// Try inserting 1 range ids. |
| 24 |
space.insertVar(VarKind::Range, 0, 1); |
| 25 |
EXPECT_EQ(space.getNumRangeVars(), 3u); |
| 26 |
} |
| 27 |
|
| 28 |
TEST(PresburgerSpaceTest, insertIdSet) { |
| 29 |
PresburgerSpace space = PresburgerSpace::getSetSpace(2, 1); |
| 30 |
|
| 31 |
// Try inserting 2 dimension ids. The space should have 4 range ids since |
| 32 |
// spaces which do not distinguish between domain, range are implemented like |
| 33 |
// this. |
| 34 |
space.insertVar(VarKind::SetDim, 0, 2); |
| 35 |
EXPECT_EQ(space.getNumRangeVars(), 4u); |
| 36 |
} |
| 37 |
|
| 38 |
TEST(PresburgerSpaceTest, removeIdRange) { |
| 39 |
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 1, 3); |
| 40 |
|
| 41 |
// Remove 1 domain identifier. |
| 42 |
space.removeVarRange(VarKind::Domain, 0, 1); |
| 43 |
EXPECT_EQ(space.getNumDomainVars(), 1u); |
| 44 |
|
| 45 |
// Remove 1 symbol and 1 range identifier. |
| 46 |
space.removeVarRange(VarKind::Symbol, 0, 1); |
| 47 |
space.removeVarRange(VarKind::Range, 0, 1); |
| 48 |
EXPECT_EQ(space.getNumDomainVars(), 1u); |
| 49 |
EXPECT_EQ(space.getNumRangeVars(), 0u); |
| 50 |
EXPECT_EQ(space.getNumSymbolVars(), 2u); |
| 51 |
} |
| 52 |
|
| 53 |
TEST(PresburgerSpaceTest, insertVarIdentifier) { |
| 54 |
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 2, 1, 0); |
| 55 |
space.resetIds<int *>(); |
| 56 |
|
| 57 |
// Attach identifiers to domain ids. |
| 58 |
int identifiers[2] = {0, 1}; |
| 59 |
space.setId<int *>(VarKind::Domain, 0, &identifiers[0]); |
| 60 |
space.setId<int *>(VarKind::Domain, 1, &identifiers[1]); |
| 61 |
|
| 62 |
// Try inserting 2 domain ids. |
| 63 |
space.insertVar(VarKind::Domain, 0, 2); |
| 64 |
EXPECT_EQ(space.getNumDomainVars(), 4u); |
| 65 |
|
| 66 |
// Try inserting 1 range ids. |
| 67 |
space.insertVar(VarKind::Range, 0, 1); |
| 68 |
EXPECT_EQ(space.getNumRangeVars(), 3u); |
| 69 |
|
| 70 |
// Check if the identifiers for the old ids are still attached properly. |
| 71 |
EXPECT_EQ(*space.getId<int *>(VarKind::Domain, 2), identifiers[0]); |
| 72 |
EXPECT_EQ(*space.getId<int *>(VarKind::Domain, 3), identifiers[1]); |
| 73 |
} |
| 74 |
|
| 75 |
TEST(PresburgerSpaceTest, removeVarRangeIdentifier) { |
| 76 |
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 1, 3, 0); |
| 77 |
space.resetIds<int *>(); |
| 78 |
|
| 79 |
int identifiers[6] = {0, 1, 2, 3, 4, 5}; |
| 80 |
|
| 81 |
// Attach identifiers to domain identifiers. |
| 82 |
space.setId<int *>(VarKind::Domain, 0, &identifiers[0]); |
| 83 |
space.setId<int *>(VarKind::Domain, 1, &identifiers[1]); |
| 84 |
|
| 85 |
// Attach identifiers to range identifiers. |
| 86 |
space.setId<int *>(VarKind::Range, 0, &identifiers[2]); |
| 87 |
|
| 88 |
// Attach identifiers to symbol identifiers. |
| 89 |
space.setId<int *>(VarKind::Symbol, 0, &identifiers[3]); |
| 90 |
space.setId<int *>(VarKind::Symbol, 1, &identifiers[4]); |
| 91 |
space.setId<int *>(VarKind::Symbol, 2, &identifiers[5]); |
| 92 |
|
| 93 |
// Remove 1 domain identifier. |
| 94 |
space.removeVarRange(VarKind::Domain, 0, 1); |
| 95 |
EXPECT_EQ(space.getNumDomainVars(), 1u); |
| 96 |
|
| 97 |
// Remove 1 symbol and 1 range identifier. |
| 98 |
space.removeVarRange(VarKind::Symbol, 0, 1); |
| 99 |
space.removeVarRange(VarKind::Range, 0, 1); |
| 100 |
EXPECT_EQ(space.getNumDomainVars(), 1u); |
| 101 |
EXPECT_EQ(space.getNumRangeVars(), 0u); |
| 102 |
EXPECT_EQ(space.getNumSymbolVars(), 2u); |
| 103 |
|
| 104 |
// Check if domain identifiers are attached properly. |
| 105 |
EXPECT_EQ(*space.getId<int *>(VarKind::Domain, 0), identifiers[1]); |
| 106 |
|
| 107 |
// Check if symbol identifiers are attached properly. |
| 108 |
EXPECT_EQ(*space.getId<int *>(VarKind::Range, 0), identifiers[4]); |
| 109 |
EXPECT_EQ(*space.getId<int *>(VarKind::Range, 1), identifiers[5]); |
| 110 |
} |