1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstdio>
17 #include <cstring>
18 #include <ctime>
19 #include <string>
20
21 #include "securec.h"
22 #include "utils.h"
23 #include "utils/log/aie_log.h"
24
25 using namespace std;
26
27 namespace {
28 const int CHAR_TYPE = 4;
29 const char MIN_UPPER_CASE_CHAR = 'A';
30 const char MIN_LOWER_CASE_CHAR = 'a';
31 const char MIN_NUMERIC_CHAR = '0';
32 const char TRAILING_CHAR = '\0';
33 const int NUMBER_OF_ALPHABETS = 26;
34 const int NUMBER_OF_DIGITS = 10;
35 const int CHAR_TYPE_UPPER_CASE = 1;
36 const int CHAR_TYPE_LOWER_CASE = 2;
37 const int CHAR_TYPE_WHITE_SPACE = 3;
38 const char WHITE_SPACE = ' ';
39 const int DIVISOR_TWO = 2;
40 const int DEFAULT_RANDOM_INT = 1;
41 }
42
43 /**
44 * Return n: 0 <= n <= max
45 */
GetRandomInt(int max)46 int GetRandomInt(int max)
47 {
48 if (max >= 0) {
49 int randomInt = rand() % (max + 1);
50 return randomInt;
51 }
52 return DEFAULT_RANDOM_INT;
53 }
54
55 /**
56 * Return true or false by random.
57 */
GetRandomBool()58 bool GetRandomBool()
59 {
60 return (rand() % DIVISOR_TWO == 1) ? true : false;
61 }
62
63 /**
64 * Gets current system date and time in format of YYYYMMDDhhmmss. E.g. 20201109123000.
65 */
GetSystemTime()66 std::string GetSystemTime()
67 {
68 time_t currentSystemTime = time(nullptr);
69 char formatTime[32] = {};
70 strftime(formatTime, sizeof(formatTime), "%Y%m%d%H%M%S", localtime(¤tSystemTime));
71 string formatTimeString = formatTime;
72 return formatTimeString;
73 }
74
75 /**
76 * Generates a string with specified length by random, which ends up with
77 * a null character '\0'. As a result, to generate a string with 20 characters,
78 * the len must be 21, and the str must point to an array of which the length is 21.
79 *
80 * len Length of string.
81 * str The string to be generated.
82 */
GetRandomString(const int len,char * str)83 void GetRandomString(const int len, char *str)
84 {
85 srand(time(nullptr));
86 for (int i = 0; i < len - 1; ++i) {
87 switch (rand() % CHAR_TYPE) {
88 case CHAR_TYPE_UPPER_CASE:
89 str[i] = MIN_UPPER_CASE_CHAR + rand() % NUMBER_OF_ALPHABETS;
90 break;
91 case CHAR_TYPE_LOWER_CASE:
92 str[i] = MIN_LOWER_CASE_CHAR + rand() % NUMBER_OF_ALPHABETS;
93 break;
94 case CHAR_TYPE_WHITE_SPACE:
95 str[i] = WHITE_SPACE;
96 break;
97 default:
98 str[i] = MIN_NUMERIC_CHAR + rand() % NUMBER_OF_DIGITS;
99 break;
100 }
101 }
102
103 str[len - 1] = TRAILING_CHAR;
104 }
105
106 /**
107 * Save characters to txt Document.
108 *
109 * input a string to be saved in the file.
110 * path path and filename of result text
111 */
SaveCharTextAppend(char * input,char * path)112 void SaveCharTextAppend(char *input, char *path)
113 {
114 HILOGI("[Test][SaveCharTextAppend]input is %s\n", input);
115 if (path == nullptr) {
116 return;
117 }
118 FILE *pFile = fopen(path, "a+");
119
120 if (pFile == nullptr) {
121 return;
122 }
123 fwrite(input, strlen(input), 1, pFile);
124 fwrite("\n", 1, 1, pFile);
125 fclose(pFile);
126 }
127
128 /**
129 * Save strings to txt Document.
130 *
131 * input a string to be saved in the file.
132 * path path and filename of result text
133 */
SaveStringTextAppend(std::string input,std::string path)134 void SaveStringTextAppend(std::string input, std::string path)
135 {
136 SaveCharTextAppend((char*)input.c_str(), (char*)path.c_str());
137 }
138