Simon Hunt | 8b28c6b | 2016-02-03 12:33:15 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Simon Hunt | 8b28c6b | 2016-02-03 12:33:15 -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 -- Test code illustrating use of trie functions |
| 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
| 24 | // injected refs |
| 25 | var $log, fs; |
| 26 | |
| 27 | // internal state |
| 28 | var out, |
| 29 | trie = {}, |
| 30 | counter = 5000; |
| 31 | |
| 32 | function write(string) { |
| 33 | out.append('div').text(string); |
| 34 | } |
| 35 | |
| 36 | function lookup(word) { |
| 37 | var result = fs.trieLookup(trie, word), |
| 38 | f = fs.isF(result), |
| 39 | show = f ? '{function}' : result; |
| 40 | |
| 41 | |
| 42 | write('------> ' + word + ' ==> ' + show); |
| 43 | |
| 44 | f && f(); |
| 45 | } |
| 46 | |
| 47 | function add(word, data) { |
| 48 | var result = fs.addToTrie(trie, word, data); |
| 49 | write(' ADD> ' + word + ' [' + data + '] ==> ' + result); |
| 50 | } |
| 51 | |
| 52 | function remove(word) { |
| 53 | var result = fs.removeFromTrie(trie, word); |
| 54 | write('REMOVE> ' + word + ' ==> ' + result); |
| 55 | } |
| 56 | |
| 57 | function func1() { |
| 58 | counter++; |
| 59 | write('** function call ** ' + counter); |
| 60 | } |
| 61 | |
| 62 | function func2() { |
| 63 | counter += 11; |
| 64 | write('** alternate call ** ' + counter); |
| 65 | } |
| 66 | |
| 67 | function runTests() { |
| 68 | lookup('cat'); |
| 69 | |
| 70 | add('cat', 101); |
| 71 | |
| 72 | lookup('ca'); |
| 73 | lookup('cat'); |
| 74 | lookup('cats'); |
| 75 | |
| 76 | add('cab', 103); |
| 77 | add('cog', 105); |
| 78 | |
| 79 | lookup('cut'); |
| 80 | lookup('cab'); |
| 81 | |
| 82 | remove('cab'); |
| 83 | |
| 84 | lookup('cab'); |
| 85 | lookup('cat'); |
| 86 | |
| 87 | add('fun', func1); |
| 88 | |
| 89 | lookup('fun'); |
| 90 | lookup('fun'); |
| 91 | lookup('fun'); |
| 92 | lookup('cat'); |
| 93 | lookup('fun'); |
| 94 | |
| 95 | add('fun', func2); |
| 96 | |
| 97 | lookup('fun'); |
| 98 | lookup('fun'); |
| 99 | lookup('fun'); |
| 100 | |
| 101 | remove('fun'); |
| 102 | |
| 103 | lookup('fun'); |
| 104 | } |
| 105 | |
| 106 | angular.module('trie', ['onosUtil']) |
| 107 | .controller('OvTrieTest', ['$log', 'FnService', |
| 108 | |
| 109 | function (_$log_, _fs_) { |
| 110 | $log = _$log_; |
| 111 | fs = _fs_; |
| 112 | out = d3.select('#output'); |
| 113 | |
| 114 | runTests(); |
| 115 | }]); |
| 116 | }()); |