|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { resetSubscriptsAndDimensions } from '../_shared/subscript' |
| 4 | + |
| 5 | +import Model from './model' |
| 6 | +import { matchingRhsRefIds } from './read-equations-expand' |
| 7 | + |
| 8 | +import { parseInlineVensimModel } from '../_tests/test-support' |
| 9 | + |
| 10 | +/** |
| 11 | + * Set up subscript/dimension state from an inline Vensim model text containing |
| 12 | + * only subscript range definitions. This is the minimal setup needed for |
| 13 | + * `indexNamesForSubscript` to work in the tests below. |
| 14 | + */ |
| 15 | +function setupSubscripts(modelText: string): void { |
| 16 | + // XXX: This is needed because subscripts are held in module-level storage |
| 17 | + resetSubscriptsAndDimensions() |
| 18 | + const parsedModel = parseInlineVensimModel(modelText) |
| 19 | + Model.read(parsedModel, /*spec=*/ {}, /*extData=*/ undefined, /*directData=*/ undefined, /*modelDir=*/ undefined, { |
| 20 | + stopAfterResolveSubscripts: true |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Build a minimal mock RHS variable instance with the given variable ID and subscripts. |
| 26 | + * The refId is synthesized from the two, e.g., `('_x', ['_a1', '_b1'])` produces |
| 27 | + * `'_x[_a1,_b1]'`. |
| 28 | + */ |
| 29 | +function rhsInstance(varId: string, subscripts: string[]): { subscripts: string[]; refId: string } { |
| 30 | + const refId = `${varId}[${subscripts.join(',')}]` |
| 31 | + return { subscripts, refId } |
| 32 | +} |
| 33 | + |
| 34 | +describe('matchingRhsRefIds', () => { |
| 35 | + it('should return all instances when LHS and RHS are both apply-to-all on a single dimension', () => { |
| 36 | + setupSubscripts(`DimA: A1, A2, A3 ~~|`) |
| 37 | + |
| 38 | + // The LHS references `_dima` at a single position; the RHS has two instances that |
| 39 | + // together cover all of `_dima`, so both should be returned. |
| 40 | + const result = matchingRhsRefIds( |
| 41 | + ['_dima'], |
| 42 | + [rhsInstance('_x', ['_a1']), rhsInstance('_x', ['_a2']), rhsInstance('_x', ['_a3'])] |
| 43 | + ) |
| 44 | + expect(result).toEqual(['_x[_a1]', '_x[_a2]', '_x[_a3]']) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should return only the matching instance when the LHS references a specific index', () => { |
| 48 | + setupSubscripts(`DimA: A1, A2, A3 ~~|`) |
| 49 | + |
| 50 | + // The LHS accesses only `_a2`, so only that RHS instance should be returned. |
| 51 | + const result = matchingRhsRefIds( |
| 52 | + ['_a2'], |
| 53 | + [rhsInstance('_x', ['_a1']), rhsInstance('_x', ['_a2']), rhsInstance('_x', ['_a3'])] |
| 54 | + ) |
| 55 | + expect(result).toEqual(['_x[_a2]']) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should return an empty array when no RHS instance overlaps with the LHS', () => { |
| 59 | + setupSubscripts(`DimA: A1, A2, A3 ~~|`) |
| 60 | + |
| 61 | + const result = matchingRhsRefIds(['_a1'], [rhsInstance('_x', ['_a2']), rhsInstance('_x', ['_a3'])]) |
| 62 | + expect(result).toEqual([]) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should return an empty array when there are no RHS instances', () => { |
| 66 | + setupSubscripts(`DimA: A1, A2 ~~|`) |
| 67 | + |
| 68 | + const result = matchingRhsRefIds(['_dima'], []) |
| 69 | + expect(result).toEqual([]) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should match at every subscript position (multi-dimensional)', () => { |
| 73 | + setupSubscripts(` |
| 74 | + DimA: A1, A2 ~~| |
| 75 | + DimB: B1, B2 ~~| |
| 76 | + DimC: C1, C2 ~~| |
| 77 | + `) |
| 78 | + |
| 79 | + // The LHS is `_dima,_c2,_dimb` (mimicking a `y[DimA,DimB,DimC] :EXCEPT: [DimA,DimB,C1]` |
| 80 | + // situation where the LHS is separated and the separated instance covers only `_c2`). |
| 81 | + // Only the RHS instance at `_c2` should match. |
| 82 | + const result = matchingRhsRefIds( |
| 83 | + ['_dima', '_c2', '_dimb'], |
| 84 | + [rhsInstance('_x', ['_dima', '_c1', '_dimb']), rhsInstance('_x', ['_dima', '_c2', '_dimb'])] |
| 85 | + ) |
| 86 | + expect(result).toEqual(['_x[_dima,_c2,_dimb]']) |
| 87 | + }) |
| 88 | + |
| 89 | + it('should require overlap at every position (no single mismatch)', () => { |
| 90 | + setupSubscripts(` |
| 91 | + DimA: A1, A2 ~~| |
| 92 | + DimB: B1, B2 ~~| |
| 93 | + `) |
| 94 | + |
| 95 | + // The LHS accesses `_a1,_b1`; the RHS instances have mismatches in at least one |
| 96 | + // position, so none should be returned. |
| 97 | + const result = matchingRhsRefIds( |
| 98 | + ['_a1', '_b1'], |
| 99 | + [rhsInstance('_x', ['_a2', '_b1']), rhsInstance('_x', ['_a1', '_b2']), rhsInstance('_x', ['_a2', '_b2'])] |
| 100 | + ) |
| 101 | + expect(result).toEqual([]) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should return multiple instances when a dimension position overlaps with each', () => { |
| 105 | + setupSubscripts(` |
| 106 | + DimA: A1, A2 ~~| |
| 107 | + DimB: B1, B2, B3 ~~| |
| 108 | + `) |
| 109 | + |
| 110 | + // LHS is fully apply-to-all; RHS is separated on DimB, so all three instances match. |
| 111 | + const result = matchingRhsRefIds( |
| 112 | + ['_dima', '_dimb'], |
| 113 | + [rhsInstance('_x', ['_dima', '_b1']), rhsInstance('_x', ['_dima', '_b2']), rhsInstance('_x', ['_dima', '_b3'])] |
| 114 | + ) |
| 115 | + expect(result).toEqual(['_x[_dima,_b1]', '_x[_dima,_b2]', '_x[_dima,_b3]']) |
| 116 | + }) |
| 117 | + |
| 118 | + it('should handle subdimensions on the LHS', () => { |
| 119 | + setupSubscripts(` |
| 120 | + DimA: A1, A2, A3, A4 ~~| |
| 121 | + SubA: A2, A3 ~~| |
| 122 | + `) |
| 123 | + |
| 124 | + // The LHS accesses the subdimension `_suba` (which covers only `_a2` and `_a3`), |
| 125 | + // so only the RHS instances at those indices should be returned. |
| 126 | + const result = matchingRhsRefIds( |
| 127 | + ['_suba'], |
| 128 | + [rhsInstance('_x', ['_a1']), rhsInstance('_x', ['_a2']), rhsInstance('_x', ['_a3']), rhsInstance('_x', ['_a4'])] |
| 129 | + ) |
| 130 | + expect(result).toEqual(['_x[_a2]', '_x[_a3]']) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should handle subdimensions on the RHS', () => { |
| 134 | + setupSubscripts(` |
| 135 | + DimA: A1, A2, A3, A4 ~~| |
| 136 | + SubA: A2, A3 ~~| |
| 137 | + `) |
| 138 | + |
| 139 | + // The LHS accesses `_a3` (a specific index); only the RHS instance whose subdimension |
| 140 | + // contains `_a3` should be returned. |
| 141 | + const result = matchingRhsRefIds( |
| 142 | + ['_a3'], |
| 143 | + [rhsInstance('_x', ['_a1']), rhsInstance('_x', ['_suba']), rhsInstance('_x', ['_a4'])] |
| 144 | + ) |
| 145 | + expect(result).toEqual(['_x[_suba]']) |
| 146 | + }) |
| 147 | + |
| 148 | + it('should return refIds in sorted order', () => { |
| 149 | + setupSubscripts(`DimA: A1, A2, A3 ~~|`) |
| 150 | + |
| 151 | + // Provide RHS instances in an unsorted order; the result should be sorted. |
| 152 | + const result = matchingRhsRefIds( |
| 153 | + ['_dima'], |
| 154 | + [rhsInstance('_x', ['_a3']), rhsInstance('_x', ['_a1']), rhsInstance('_x', ['_a2'])] |
| 155 | + ) |
| 156 | + expect(result).toEqual(['_x[_a1]', '_x[_a2]', '_x[_a3]']) |
| 157 | + }) |
| 158 | + |
| 159 | + it('should efficiently handle large dimension sizes', () => { |
| 160 | + // Generate a model with three large dimensions, similar in spirit to the original |
| 161 | + // performance bug (36 × 56 × 22). The old implementation was O(product of sizes) |
| 162 | + // which would make this test slow; the new implementation is O(sum of sizes). |
| 163 | + const dimASize = 40 |
| 164 | + const dimBSize = 60 |
| 165 | + const dimCSize = 25 |
| 166 | + const dimASubs = Array.from({ length: dimASize }, (_, i) => `A${i + 1}`) |
| 167 | + const dimBSubs = Array.from({ length: dimBSize }, (_, i) => `B${i + 1}`) |
| 168 | + const dimCSubs = Array.from({ length: dimCSize }, (_, i) => `C${i + 1}`) |
| 169 | + setupSubscripts(` |
| 170 | + DimA: ${dimASubs.join(', ')} ~~| |
| 171 | + DimB: ${dimBSubs.join(', ')} ~~| |
| 172 | + DimC: ${dimCSubs.join(', ')} ~~| |
| 173 | + `) |
| 174 | + |
| 175 | + // One RHS instance covering the entire cartesian product, plus a decoy instance |
| 176 | + // that has a mismatch at a single position. |
| 177 | + const rhsVarInstances = [ |
| 178 | + rhsInstance('_x', ['_dima', '_dimb', '_dimc']), |
| 179 | + rhsInstance('_x', ['_dima', '_dimb', '_c1']) |
| 180 | + ] |
| 181 | + |
| 182 | + // LHS accesses `_c2`, so only the first instance should match (the decoy has |
| 183 | + // only `_c1` at position 2). |
| 184 | + const result = matchingRhsRefIds(['_dima', '_dimb', '_c2'], rhsVarInstances) |
| 185 | + expect(result).toEqual(['_x[_dima,_dimb,_dimc]']) |
| 186 | + }) |
| 187 | +}) |
0 commit comments