Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "utils/common.h"
using namespace std;
using namespace MxRec;
using namespace testing;
TEST(common, InitializeInfo)
{
NormalInitializerInfo nInfoTruncatedNormal;
string nameTruncatedNormal = "truncated_normal_initializer";
InitializeInfo iInfo = InitializeInfo(nameTruncatedNormal, 0, 1, nInfoTruncatedNormal);
ASSERT_EQ(iInfo.initializerType, InitializerType::TRUNCATED_NORMAL);
NormalInitializerInfo nInfoRandomNormal;
string nameRandomNormal = "random_normal_initializer";
iInfo = InitializeInfo(nameRandomNormal, 0, 1, nInfoRandomNormal);
ASSERT_EQ(iInfo.initializerType, InitializerType::RANDOM_NORMAL);
NormalInitializerInfo nInfoInvalid;
string nameInvalid = "x";
bool isExceptionThrow = {false};
try {
iInfo = InitializeInfo(nameInvalid, 0, 1, nInfoInvalid);
} catch (const std::invalid_argument& e) {
isExceptionThrow = true;
}
ASSERT_EQ(isExceptionThrow, true);
}
TEST(TestRandomInfo, Basic)
{
MxRec::RandomInfo info(0, 10, 1.0f, 0.0f, 1.0f);
EXPECT_EQ(info.start, 0);
EXPECT_EQ(info.len, 10);
EXPECT_EQ(info.constantVal, 1.0f);
EXPECT_EQ(info.randomMin, 0.0f);
EXPECT_EQ(info.randomMax, 1.0f);
}
TEST(TestSetLog, Basic)
{
MxRec::GlogConfig::gRankId = "";
MxRec::SetLog(0);
EXPECT_EQ(MxRec::GlogConfig::gGlogLevel, GlobalEnv::glogStderrthreshold);
EXPECT_EQ(MxRec::GlogConfig::gRankId, "0");
}
TEST(TestSetLog, GRankIdNotEmpty)
{
MxRec::GlogConfig::gRankId = "1";
MxRec::SetLog(0);
EXPECT_EQ(MxRec::GlogConfig::gRankId, "1");
}
TEST(TestGetThreadNumEnv, Basic)
{
int num = MxRec::GetThreadNumEnv();
EXPECT_EQ(num, GlobalEnv::keyProcessThreadNum);
}
TEST(TestValidateReadFile, Basic)
{
EXPECT_NO_THROW(MxRec::ValidateReadFile("/home/slice_0.data", 28000000));
}
TEST(TestValidateReadFile, InvalidDatasetSize_Error)
{
long long invalidSize = 1LL << 40 + 1;
EXPECT_THROW(MxRec::ValidateReadFile("/home/slice_0.data", invalidSize), invalid_argument);
}
TEST(RankInfoTest, ConstructOk)
{
auto maxSteps = vector<int>{100};
auto rankInfo = RankInfo(1, 1, maxSteps);
EXPECT_EQ(rankInfo.localRankSize, 1);
}
TEST(RankInfoTest, LocalRankSizeZero)
{
auto maxSteps = vector<int>{100};
auto rankInfo = RankInfo(0, 1, maxSteps);
EXPECT_EQ(rankInfo.localRankSize, 0);
}
TEST(RankInfoTest, LocalRankSizeNotZero)
{
auto ctrlSteps = vector<int>{100};
auto rankInfo = RankInfo(1, 1, 1, 1, ctrlSteps);
EXPECT_EQ(rankInfo.localRankSize, 1);
}
TEST(CommTest, CheckFilePermissionErr)
{
auto filePath = string("/etc/os-release");
EXPECT_EQ(CheckFilePermission(filePath), false);
filePath = string("/etc/unknown");
EXPECT_EQ(CheckFilePermission(filePath), false);
}
TEST(CommTest, FloatPtrToLimitStrOk)
{
float val = 0;
auto ptrVal = &val;
auto strVal = FloatPtrToLimitStr(ptrVal, 1);
EXPECT_EQ(strVal, "0.000000 ");
}
TEST(CommTest, GetStepFromPathOk) {
const auto loadPath = SAVE_SPARSE_PATH_PREFIX + "-ckpt-0";
auto step = GetStepFromPath(loadPath);
EXPECT_EQ(step, 0);
}
TEST(CommTest, CheckFileExistOk) {
const auto filePath = "invalid_path"s;
EXPECT_FALSE(CheckFileExist(filePath));
}