11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst result = process.cpuUsage(); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// Validate the result of calling with no previous value argument. 71cb0ef41Sopenharmony_civalidateResult(result); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci// Validate the result of calling with a previous value argument. 101cb0ef41Sopenharmony_civalidateResult(process.cpuUsage(result)); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci// Ensure the results are >= the previous. 131cb0ef41Sopenharmony_cilet thisUsage; 141cb0ef41Sopenharmony_cilet lastUsage = process.cpuUsage(); 151cb0ef41Sopenharmony_cifor (let i = 0; i < 10; i++) { 161cb0ef41Sopenharmony_ci thisUsage = process.cpuUsage(); 171cb0ef41Sopenharmony_ci validateResult(thisUsage); 181cb0ef41Sopenharmony_ci assert(thisUsage.user >= lastUsage.user); 191cb0ef41Sopenharmony_ci assert(thisUsage.system >= lastUsage.system); 201cb0ef41Sopenharmony_ci lastUsage = thisUsage; 211cb0ef41Sopenharmony_ci} 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci// Ensure that the diffs are >= 0. 241cb0ef41Sopenharmony_cilet startUsage; 251cb0ef41Sopenharmony_cilet diffUsage; 261cb0ef41Sopenharmony_cifor (let i = 0; i < 10; i++) { 271cb0ef41Sopenharmony_ci startUsage = process.cpuUsage(); 281cb0ef41Sopenharmony_ci diffUsage = process.cpuUsage(startUsage); 291cb0ef41Sopenharmony_ci validateResult(startUsage); 301cb0ef41Sopenharmony_ci validateResult(diffUsage); 311cb0ef41Sopenharmony_ci assert(diffUsage.user >= 0); 321cb0ef41Sopenharmony_ci assert(diffUsage.system >= 0); 331cb0ef41Sopenharmony_ci} 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci// Ensure that an invalid shape for the previous value argument throws an error. 361cb0ef41Sopenharmony_ciassert.throws( 371cb0ef41Sopenharmony_ci () => process.cpuUsage(1), 381cb0ef41Sopenharmony_ci { 391cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 401cb0ef41Sopenharmony_ci name: 'TypeError', 411cb0ef41Sopenharmony_ci message: 'The "prevValue" argument must be of type object. ' + 421cb0ef41Sopenharmony_ci 'Received type number (1)' 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci// Check invalid types. 471cb0ef41Sopenharmony_ci[ 481cb0ef41Sopenharmony_ci {}, 491cb0ef41Sopenharmony_ci { user: 'a' }, 501cb0ef41Sopenharmony_ci { user: null, system: 'c' }, 511cb0ef41Sopenharmony_ci].forEach((value) => { 521cb0ef41Sopenharmony_ci assert.throws( 531cb0ef41Sopenharmony_ci () => process.cpuUsage(value), 541cb0ef41Sopenharmony_ci { 551cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 561cb0ef41Sopenharmony_ci name: 'TypeError', 571cb0ef41Sopenharmony_ci message: 'The "prevValue.user" property must be of type number.' + 581cb0ef41Sopenharmony_ci common.invalidArgTypeHelper(value.user) 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci ); 611cb0ef41Sopenharmony_ci}); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci[ 641cb0ef41Sopenharmony_ci { user: 3, system: 'b' }, 651cb0ef41Sopenharmony_ci { user: 3, system: null }, 661cb0ef41Sopenharmony_ci].forEach((value) => { 671cb0ef41Sopenharmony_ci assert.throws( 681cb0ef41Sopenharmony_ci () => process.cpuUsage(value), 691cb0ef41Sopenharmony_ci { 701cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 711cb0ef41Sopenharmony_ci name: 'TypeError', 721cb0ef41Sopenharmony_ci message: 'The "prevValue.system" property must be of type number.' + 731cb0ef41Sopenharmony_ci common.invalidArgTypeHelper(value.system) 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci ); 761cb0ef41Sopenharmony_ci}); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci// Check invalid values. 791cb0ef41Sopenharmony_ci[ 801cb0ef41Sopenharmony_ci { user: -1, system: 2 }, 811cb0ef41Sopenharmony_ci { user: Number.POSITIVE_INFINITY, system: 4 }, 821cb0ef41Sopenharmony_ci].forEach((value) => { 831cb0ef41Sopenharmony_ci assert.throws( 841cb0ef41Sopenharmony_ci () => process.cpuUsage(value), 851cb0ef41Sopenharmony_ci { 861cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE', 871cb0ef41Sopenharmony_ci name: 'RangeError', 881cb0ef41Sopenharmony_ci message: "The property 'prevValue.user' is invalid. " + 891cb0ef41Sopenharmony_ci `Received ${value.user}`, 901cb0ef41Sopenharmony_ci } 911cb0ef41Sopenharmony_ci ); 921cb0ef41Sopenharmony_ci}); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci[ 951cb0ef41Sopenharmony_ci { user: 3, system: -2 }, 961cb0ef41Sopenharmony_ci { user: 5, system: Number.NEGATIVE_INFINITY }, 971cb0ef41Sopenharmony_ci].forEach((value) => { 981cb0ef41Sopenharmony_ci assert.throws( 991cb0ef41Sopenharmony_ci () => process.cpuUsage(value), 1001cb0ef41Sopenharmony_ci { 1011cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE', 1021cb0ef41Sopenharmony_ci name: 'RangeError', 1031cb0ef41Sopenharmony_ci message: "The property 'prevValue.system' is invalid. " + 1041cb0ef41Sopenharmony_ci `Received ${value.system}`, 1051cb0ef41Sopenharmony_ci } 1061cb0ef41Sopenharmony_ci ); 1071cb0ef41Sopenharmony_ci}); 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci// Ensure that the return value is the expected shape. 1101cb0ef41Sopenharmony_cifunction validateResult(result) { 1111cb0ef41Sopenharmony_ci assert.notStrictEqual(result, null); 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci assert(Number.isFinite(result.user)); 1141cb0ef41Sopenharmony_ci assert(Number.isFinite(result.system)); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci assert(result.user >= 0); 1171cb0ef41Sopenharmony_ci assert(result.system >= 0); 1181cb0ef41Sopenharmony_ci} 119