#include "llvm/DebugInfo/MSF/MSFCommon.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace llvm::msf;
TEST(MSFCommonTest, BytesToBlocks) {
EXPECT_EQ(0ULL, bytesToBlocks(0, 4096));
EXPECT_EQ(1ULL, bytesToBlocks(12, 4096));
EXPECT_EQ(1ULL, bytesToBlocks(4096, 4096));
EXPECT_EQ(2ULL, bytesToBlocks(4097, 4096));
EXPECT_EQ(2ULL, bytesToBlocks(600, 512));
}
TEST(MSFCommonTest, FpmIntervals) {
SuperBlock SB;
SB.FreeBlockMapBlock = 1;
SB.BlockSize = 4096;
MSFLayout L;
L.SB = &SB;
SB.NumBlocks = 12;
EXPECT_EQ(1u, getNumFpmIntervals(L, false));
SB.NumBlocks = SB.BlockSize;
EXPECT_EQ(1u, getNumFpmIntervals(L, false));
SB.NumBlocks = SB.BlockSize + 1;
EXPECT_EQ(1u, getNumFpmIntervals(L, false));
SB.NumBlocks = SB.BlockSize * 8;
EXPECT_EQ(1u, getNumFpmIntervals(L, false));
SB.NumBlocks = SB.BlockSize * 8 + 1;
EXPECT_EQ(2u, getNumFpmIntervals(L, false));
SB.NumBlocks = 12;
EXPECT_EQ(1u, getNumFpmIntervals(L, true));
SB.NumBlocks = SB.BlockSize;
EXPECT_EQ(1u, getNumFpmIntervals(L, true));
SB.NumBlocks = SB.BlockSize * 8;
EXPECT_EQ(8u, getNumFpmIntervals(L, true));
for (uint32_t I : {4096, 4097}) {
EXPECT_EQ(1U, getNumFpmIntervals(4096, I, true, 1));
EXPECT_EQ(1U, getNumFpmIntervals(4096, I, false, 1));
EXPECT_EQ(1U, getNumFpmIntervals(4096, I, true, 2));
EXPECT_EQ(1U, getNumFpmIntervals(4096, I, false, 2));
}
EXPECT_EQ(2U, getNumFpmIntervals(4096, 4098, true, 1));
EXPECT_EQ(1U, getNumFpmIntervals(4096, 4098, false, 1));
EXPECT_EQ(1U, getNumFpmIntervals(4096, 4098, true, 2));
EXPECT_EQ(1U, getNumFpmIntervals(4096, 4098, false, 2));
EXPECT_EQ(2U, getNumFpmIntervals(4096, 4099, true, 1));
EXPECT_EQ(1U, getNumFpmIntervals(4096, 4099, false, 1));
EXPECT_EQ(2U, getNumFpmIntervals(4096, 4099, true, 2));
EXPECT_EQ(1U, getNumFpmIntervals(4096, 4099, false, 2));
}
TEST(MSFCommonTest, FpmStreamLayout) {
SuperBlock SB;
MSFLayout L;
L.SB = &SB;
SB.FreeBlockMapBlock = 1;
SB.BlockSize = 4096;
SB.NumBlocks = 8000;
MSFStreamLayout SL = getFpmStreamLayout(L, false, false);
EXPECT_EQ(1000u, SL.Length);
EXPECT_EQ(1u, SL.Blocks.size());
EXPECT_EQ(SB.FreeBlockMapBlock, SL.Blocks.front());
SL = getFpmStreamLayout(L, false, true);
EXPECT_EQ(1000u, SL.Length);
EXPECT_EQ(1u, SL.Blocks.size());
EXPECT_EQ(3u - SB.FreeBlockMapBlock, SL.Blocks.front());
SB.NumBlocks = SB.BlockSize * 8 + 1;
SL = getFpmStreamLayout(L, false, false);
EXPECT_EQ(SB.BlockSize + 1, SL.Length);
EXPECT_EQ(2u, SL.Blocks.size());
EXPECT_EQ(SB.FreeBlockMapBlock, SL.Blocks[0]);
EXPECT_EQ(SB.FreeBlockMapBlock + SB.BlockSize, SL.Blocks[1]);
SL = getFpmStreamLayout(L, false, true);
EXPECT_EQ(SB.BlockSize + 1, SL.Length);
EXPECT_EQ(2u, SL.Blocks.size());
EXPECT_EQ(3u - SB.FreeBlockMapBlock, SL.Blocks[0]);
EXPECT_EQ(3u - SB.FreeBlockMapBlock + SB.BlockSize, SL.Blocks[1]);
SB.NumBlocks = SB.BlockSize * 8 + 3;
SL = getFpmStreamLayout(L, true, false);
EXPECT_EQ(SB.BlockSize * 9, SL.Length);
EXPECT_EQ(9u, SL.Blocks.size());
for (int I = 0; I < 9; ++I)
EXPECT_EQ(I * SB.BlockSize + SB.FreeBlockMapBlock, SL.Blocks[I]);
}