Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- Util -- Random Service - Unit Tests |
| 19 | */ |
| 20 | describe('factory: fw/util/random.js', function() { |
| 21 | var rnd, $log, fs; |
| 22 | |
| 23 | beforeEach(module('onosUtil')); |
| 24 | |
| 25 | beforeEach(inject(function (RandomService, _$log_, FnService) { |
| 26 | rnd = RandomService; |
| 27 | $log = _$log_; |
| 28 | fs = FnService; |
| 29 | })); |
| 30 | |
| 31 | // interesting use of a custom matcher... |
| 32 | beforeEach(function () { |
| 33 | jasmine.addMatchers({ |
| 34 | toBeWithinOf: function () { |
| 35 | return { |
| 36 | compare: function (actual, distance, base) { |
| 37 | var lower = base - distance, |
| 38 | upper = base + distance, |
| 39 | result = {}; |
| 40 | |
| 41 | result.pass = Math.abs(actual - base) <= distance; |
| 42 | |
| 43 | if (result.pass) { |
| 44 | // for negation with ".not" |
| 45 | result.message = 'Expected ' + actual + |
| 46 | ' to be outside ' + lower + ' and ' + |
| 47 | upper + ' (inclusive)'; |
| 48 | } else { |
| 49 | result.message = 'Expected ' + actual + |
| 50 | ' to be between ' + lower + ' and ' + |
| 51 | upper + ' (inclusive)'; |
| 52 | } |
| 53 | return result; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | it('should define RandomService', function () { |
| 61 | expect(rnd).toBeDefined(); |
| 62 | }); |
| 63 | |
| 64 | it('should define api functions', function () { |
| 65 | expect(fs.areFunctions(rnd, [ |
| 66 | 'spread', 'randDim' |
| 67 | ])).toBeTruthy(); |
| 68 | }); |
| 69 | |
| 70 | // really, can only do this heuristically.. hope this doesn't break |
| 71 | it('should spread results across the range', function () { |
| 72 | var load = 1000, |
| 73 | s = 12, |
| 74 | low = 0, |
| 75 | high = 0, |
| 76 | i, res, |
| 77 | which = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 78 | minCount = load / s * 0.5; // generous error |
| 79 | |
| 80 | for (i=0; i<load; i++) { |
| 81 | res = rnd.spread(s); |
| 82 | if (res < low) low = res; |
| 83 | if (res > high) high = res; |
| 84 | which[res + s/2]++; |
| 85 | } |
| 86 | expect(low).toBe(-6); |
| 87 | expect(high).toBe(5); |
| 88 | |
| 89 | // check we got a good number of hits in each bucket |
| 90 | for (i=0; i<s; i++) { |
| 91 | expect(which[i]).toBeGreaterThan(minCount); |
| 92 | } |
| 93 | }); |
| 94 | |
| 95 | // really, can only do this heuristically.. hope this doesn't break |
| 96 | it('should choose results across the dimension', function () { |
| 97 | var load = 1000, |
| 98 | dim = 100, |
| 99 | low = 999, |
| 100 | high = 0, |
| 101 | i, res; |
| 102 | |
| 103 | for (i=0; i<load; i++) { |
| 104 | res = rnd.randDim(dim); |
| 105 | if (res < low) low = res; |
| 106 | if (res > high) high = res; |
| 107 | expect(res).toBeWithinOf(36, 50); |
| 108 | } |
| 109 | }); |
| 110 | }); |