1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23require('../common'); 24const assert = require('assert'); 25 26// The default behavior, return an Array "tuple" of numbers 27const tuple = process.hrtime(); 28 29// Validate the default behavior 30validateTuple(tuple); 31 32// Validate that passing an existing tuple returns another valid tuple 33validateTuple(process.hrtime(tuple)); 34 35// Test that only an Array may be passed to process.hrtime() 36assert.throws(() => { 37 process.hrtime(1); 38}, { 39 code: 'ERR_INVALID_ARG_TYPE', 40 name: 'TypeError', 41 message: 'The "time" argument must be an instance of Array. Received type ' + 42 'number (1)' 43}); 44assert.throws(() => { 45 process.hrtime([]); 46}, { 47 code: 'ERR_OUT_OF_RANGE', 48 name: 'RangeError', 49 message: 'The value of "time" is out of range. It must be 2. Received 0' 50}); 51assert.throws(() => { 52 process.hrtime([1]); 53}, { 54 code: 'ERR_OUT_OF_RANGE', 55 name: 'RangeError', 56 message: 'The value of "time" is out of range. It must be 2. Received 1' 57}); 58assert.throws(() => { 59 process.hrtime([1, 2, 3]); 60}, { 61 code: 'ERR_OUT_OF_RANGE', 62 name: 'RangeError', 63 message: 'The value of "time" is out of range. It must be 2. Received 3' 64}); 65 66function validateTuple(tuple) { 67 assert(Array.isArray(tuple)); 68 assert.strictEqual(tuple.length, 2); 69 assert(Number.isInteger(tuple[0])); 70 assert(Number.isInteger(tuple[1])); 71} 72 73const diff = process.hrtime([0, 1e9 - 1]); 74assert(diff[1] >= 0); // https://github.com/nodejs/node/issues/4751 75