ActsUtilMathApiTest.cpp 5.4 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 * 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 <stdlib.h>

#include "gtest/gtest.h"
#include "log.h"
#include "utils.h"

using namespace testing::ext;

class ActsUtilMathApiTest : public testing::Test {
protected:
    // SetUpTestCase: Testsuit setup, run before 1st testcase
    static void SetUpTestCase(void)
    {
    }
    // TearDownTestCase: Testsuit teardown, run after last testcase
    static void TearDownTestCase(void)
    {
    }
    // Testcase setup
    virtual void SetUp()
    {
    }
    // Testcase teardown
    virtual void TearDown()
    {
    }
};

/**
* @tc.number     SUB_KERNEL_UTIL_MATH_DRAND48_0100
* @tc.name       test drand48 api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
49
HWTEST_F(ActsUtilMathApiTest, testDrand480100, Function | MediumTest | Level1) {
W
wenjun 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    double returnVal;
    long seedVal;

    seedVal = 12345;
    srand48(seedVal);
    returnVal = drand48();
    LOGD("    drand48 returnVal:='%f'\n", returnVal);
    ASSERT_TRUE(returnVal >= 0.0 && returnVal < 1.0) << "ErrInfo: drand48 returnVal:='" << returnVal << "'";
}

/**
* @tc.number     SUB_KERNEL_UTIL_MATH_ERAND48_0200
* @tc.name       test erand48 api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
65
HWTEST_F(ActsUtilMathApiTest, testErand480200, Function | MediumTest | Level1) {
W
wenjun 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78
    double returnVal;
    unsigned short xsubi[3] = {0, 1, 2};

    returnVal = erand48(xsubi);
    LOGD("    erand48 returnVal:='%f'\n", returnVal);
    ASSERT_TRUE(returnVal >= 0.0 && returnVal < 1.0) << "ErrInfo: erand48 returnVal:='" << returnVal << "'";
}

/**
* @tc.number     SUB_KERNEL_UTIL_MATH_JRAND48_0300
* @tc.name       test jrand48 api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
79
HWTEST_F(ActsUtilMathApiTest, testJrand480300, Function | MediumTest | Level1) {
80
    long long returnVal;
W
wenjun 已提交
81 82 83 84
    unsigned short xsubi[3] = {0, 1, 2};

    returnVal = jrand48(xsubi);
    LOGD("    jrand48 returnVal:='%ld'\n", returnVal);
85
    ASSERT_TRUE(returnVal >= -2147483648 && returnVal < 2147483648)
W
wenjun 已提交
86 87 88 89
        << "ErrInfo: jrand48 returnVal:='" << returnVal << "'";
}

/**
M
mamingshuai 已提交
90
* @tc.number     SUB_KERNEL_UTIL_MATH_MRAND48_0400
W
wenjun 已提交
91 92 93
* @tc.name       test mrand48 api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
94
HWTEST_F(ActsUtilMathApiTest, testMrand480400, Function | MediumTest | Level1) {
95
    long long returnVal;
W
wenjun 已提交
96 97 98 99 100
    unsigned short paraVal[7] = {0, 1, 2, 3, 4, 5, 6};

    lcong48(paraVal);
    returnVal = mrand48();
    LOGD("    mrand48 returnVal:='%ld'\n", returnVal);
101
    ASSERT_TRUE(returnVal >= -2147483648 && returnVal < 2147483648)
W
wenjun 已提交
102 103 104 105
        << "ErrInfo: mrand48 returnVal:='" << returnVal << "'";
}

/**
M
mamingshuai 已提交
106
* @tc.number     SUB_KERNEL_UTIL_MATH_LRAND48_0500
W
wenjun 已提交
107 108 109
* @tc.name       test lrand48 api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
110
HWTEST_F(ActsUtilMathApiTest, testLrand480500, Function | MediumTest | Level1) {
111
    long long returnVal;
W
wenjun 已提交
112 113 114 115 116
    unsigned short paraVal[3] = {0, 1, 2};

    seed48(paraVal);
    returnVal = lrand48();
    LOGD("    lrand48 returnVal:='%ld'\n", returnVal);
117
    ASSERT_TRUE(returnVal >= 0 && returnVal < 2147483648) << "ErrInfo: lrand48 returnVal:='" << returnVal << "'";
W
wenjun 已提交
118 119
}

M
mamingshuai 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/**
* @tc.number     SUB_KERNEL_UTIL_MATH_SRANDOM_0600
* @tc.name       test srandom api
* @tc.desc       [C- SOFTWARE -0200]
*/
HWTEST_F(ActsUtilMathApiTest, testSrandom0600, Function | MediumTest | Level1) {
    char *returnVal = nullptr;
    unsigned int seedVal;
    char stateVal[8];
    size_t stateSize;

    seedVal = 1;
    stateSize = 8;
    srandom(seedVal);
    returnVal = initstate(seedVal, stateVal, stateSize);
    LOGD("    initstate returnVal:='%x'\n", returnVal);
    ASSERT_TRUE(returnVal != nullptr) << "ErrInfo: srandom returnVal:='" << returnVal << "'";
    returnVal = setstate(stateVal);
    LOGD("    setstate returnVal:='%x'\n", returnVal);
    ASSERT_TRUE(returnVal != nullptr) << "ErrInfo: srandom returnVal:='" << returnVal << "'";
}

W
wenjun 已提交
142 143 144 145 146
/**
* @tc.number     SUB_KERNEL_UTIL_MATH_NRAND48_0700
* @tc.name       test nrand48 api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
147
HWTEST_F(ActsUtilMathApiTest, testNrand480700, Function | MediumTest | Level1) {
148
    long long returnVal;
W
wenjun 已提交
149 150 151 152
    unsigned short paraVal[3] = {0, 1, 2};

    returnVal = nrand48(paraVal);
    LOGD("    nrand48 returnVal:='%ld'\n", returnVal);
153
    ASSERT_TRUE(returnVal >= 0 && returnVal < 2147483648) << "ErrInfo: nrand48 returnVal:='" << returnVal << "'";
W
wenjun 已提交
154 155 156 157 158 159 160
}

/**
* @tc.number     SUB_KERNEL_UTIL_MATH_RAND_R_0800
* @tc.name       test rand_r api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
161
HWTEST_F(ActsUtilMathApiTest, testNrand480800, Function | MediumTest | Level1) {
W
wenjun 已提交
162 163 164 165 166 167
    int returnVal;
    unsigned int paraVal;

    returnVal = rand_r(&paraVal);
    LOGD("    rand_r returnVal:='%ld'\n", returnVal);
    ASSERT_TRUE(returnVal >= 0 && returnVal <= RAND_MAX) << "ErrInfo: rand_r returnVal:='" << returnVal << "'";
M
mamingshuai 已提交
168
}