• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022-2023 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
16import fileIO from '@ohos.file.fs';
17import util from '@ohos.util';
18import featureAbility from '@ohos.ability.featureAbility';
19
20export const FILE_CONTENT = 'hello world';
21
22import {
23  describe, it, expect
24}
25from '@ohos/hypium'
26
27export function prepareFile(fpath, content) {
28  try {
29    let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE)
30    fileIO.truncateSync(file.fd)
31    fileIO.writeSync(file.fd, content)
32    fileIO.fsyncSync(file.fd)
33    fileIO.closeSync(file)
34    return true
35  }
36  catch (e) {
37    console.log('Failed to prepareFile for ' + e)
38    return false
39  }
40}
41
42export function prepare200MFile(fpath) {
43  try {
44    let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE)
45    fileIO.truncateSync(file.fd)
46    let bf = new ArrayBuffer(1024 * 1024 * 20);
47    for (let i = 0; i < 10; i++) {
48      let offset = bf.byteLength * i;
49      let writeLen = fileIO.writeSync(file.fd, bf, { offset: offset, length: bf.byteLength, encoding: 'utf-8' });
50    }
51    fileIO.fsyncSync(file.fd)
52    fileIO.closeSync(file)
53    return true
54  }
55  catch (e) {
56    console.log('Failed to prepare200MFile for ' + e)
57    return false
58  }
59}
60
61export async function nextFileName(testName) {
62  let context = featureAbility.getContext();
63  let data = await context.getCacheDir();
64  let BASE_PATH = data + '/';
65  return BASE_PATH + testName + '_' + randomString(testName.length);
66}
67
68export async function fileName(testName) {
69  let context = featureAbility.getContext();
70  let data = await context.getFilesDir();
71  let BASE_PATH = data + '/';
72  return BASE_PATH + testName + '_' + randomString(testName.length);
73}
74
75export function randomString(num) {
76  let len= num;
77  var $chars = 'aaaabbbbcccc';
78  var maxPos = $chars.length;
79  var pwd = '';
80  for (var i = 0; i < len; i++) {
81    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
82  }
83  return pwd;
84}
85
86export async function sleep(times) {
87  if (!times) {
88      times = 10;
89  }
90  await new Promise((res) => setTimeout(res, times));
91}
92
93function isIntNum(val) {
94  return typeof val === 'number' && val % 1 === 0;
95}
96
97function isBigInt(val) {
98  return typeof val === 'bigint';
99}
100
101function isString(str) {
102  return (typeof str == 'string') && str.constructor == String;
103}
104
105export {
106  fileIO,
107  isIntNum,
108  isBigInt,
109  isString,
110  describe,
111  it,
112  expect,
113  util
114};