|
| 1 | +import { afterAll, beforeAll, describe } from 'vitest'; |
| 2 | +import { AccessType, ContainerType, setupContainerFunctions } from '../../_helper/pointer-analysis'; |
| 3 | +import { assertDataflow, withShell } from '../../_helper/shell'; |
| 4 | +import { label } from '../../_helper/label'; |
| 5 | +import { emptyGraph } from '../../../../src/dataflow/graph/dataflowgraph-builder'; |
| 6 | +import { Q } from '../../../../src/search/flowr-search-builder'; |
| 7 | +import { amendConfig, defaultConfigOptions } from '../../../../src/config'; |
| 8 | + |
| 9 | +describe.sequential('Container Single Index Based Access', withShell(shell => { |
| 10 | + describe.each( |
| 11 | + [ |
| 12 | + { container: ContainerType.Vector, type: AccessType.DoubleBracket, hasNamedArguments: false }, |
| 13 | + { container: ContainerType.Vector, type: AccessType.SingleBracket, hasNamedArguments: false }, |
| 14 | + { container: ContainerType.List, type: AccessType.DoubleBracket, hasNamedArguments: false }, |
| 15 | + { container: ContainerType.List, type: AccessType.SingleBracket, hasNamedArguments: false }, |
| 16 | + { container: ContainerType.List, type: AccessType.DoubleBracket, hasNamedArguments: true }, |
| 17 | + { container: ContainerType.List, type: AccessType.SingleBracket, hasNamedArguments: true }, |
| 18 | + { container: ContainerType.List, type: AccessType.Dollar, hasNamedArguments: true }, |
| 19 | + ] |
| 20 | + )('Access for container $container using $type and hasNamedArguments $hasNamedArguments', ({ container, type, hasNamedArguments }) => { |
| 21 | + const { |
| 22 | + acc, def, accessCapability, queryArg, queryNamedArg, queryUnnamedArg, queryAccInLine |
| 23 | + } = setupContainerFunctions(container, type, hasNamedArguments); |
| 24 | + |
| 25 | + const basicCapabilities = [ |
| 26 | + 'name-normal', |
| 27 | + 'function-calls', |
| 28 | + hasNamedArguments ? 'named-arguments' : 'unnamed-arguments', |
| 29 | + 'subsetting-multiple', |
| 30 | + accessCapability, |
| 31 | + ] as const; |
| 32 | + |
| 33 | + beforeAll(() => { |
| 34 | + amendConfig({ solver: { ...defaultConfigOptions.solver, pointerTracking: true } }); |
| 35 | + }); |
| 36 | + |
| 37 | + afterAll(() => { |
| 38 | + amendConfig({ solver: { ...defaultConfigOptions.solver, pointerTracking: false } }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('Simple access', () => { |
| 42 | + assertDataflow( |
| 43 | + label('When single index is accessed, then access reads index', basicCapabilities), |
| 44 | + shell, |
| 45 | + `numbers <- ${def('1', '2')} |
| 46 | + ${acc('numbers', 2)}`, |
| 47 | + (data) => emptyGraph() |
| 48 | + .readsQuery(queryAccInLine(2), queryArg(2, '2', 1), data), |
| 49 | + { |
| 50 | + expectIsSubgraph: true, |
| 51 | + resolveIdsAsCriterion: true, |
| 52 | + } |
| 53 | + ); |
| 54 | + |
| 55 | + describe.skipIf(container !== ContainerType.Vector)('Flattened Vectors', () => { |
| 56 | + assertDataflow( |
| 57 | + label('When single flattened index is accessed, then access reads index', basicCapabilities), |
| 58 | + shell, |
| 59 | + `numbers <- ${def('1', 'c(2, 3)', '4')} |
| 60 | + ${acc('numbers', 2)}`, |
| 61 | + (data) => emptyGraph() |
| 62 | + .readsQuery(queryAccInLine(2), queryUnnamedArg('2', 1), data), |
| 63 | + { |
| 64 | + expectIsSubgraph: true, |
| 65 | + resolveIdsAsCriterion: true, |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + assertDataflow( |
| 70 | + label('When single named flattened list index is accessed, then access reads index', [...basicCapabilities, 'named-arguments']), |
| 71 | + shell, |
| 72 | + `numbers <- ${def('1', 'list(a = 2, b = 3)', '4')} |
| 73 | + ${acc('numbers', 2)}`, |
| 74 | + (data) => emptyGraph() |
| 75 | + .readsQuery(queryAccInLine(2), queryNamedArg('a', 1), data), |
| 76 | + { |
| 77 | + expectIsSubgraph: true, |
| 78 | + resolveIdsAsCriterion: true, |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + assertDataflow( |
| 83 | + label('When single unnamed flattened list index is accessed, then access reads index', basicCapabilities), |
| 84 | + shell, |
| 85 | + `numbers <- ${def('1', 'list(2, 3)', '4')} |
| 86 | + ${acc('numbers', 2)}`, |
| 87 | + (data) => emptyGraph() |
| 88 | + .readsQuery(queryAccInLine(2), queryUnnamedArg('2', 1), data), |
| 89 | + { |
| 90 | + expectIsSubgraph: true, |
| 91 | + resolveIdsAsCriterion: true, |
| 92 | + } |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + describe.skipIf(container !== ContainerType.List)('Nested Lists', () => { |
| 97 | + assertDataflow( |
| 98 | + label('When single nested index is accessed, then access reads index', basicCapabilities), |
| 99 | + shell, |
| 100 | + `numbers <- ${def('1', ['2', '3'], '4')} |
| 101 | + ${acc('numbers', 2, 1)}`, |
| 102 | + (data) => emptyGraph() |
| 103 | + .readsQuery({ query: Q.varInLine(type, 2).last() }, queryArg(3, '2', 1), data), |
| 104 | + { |
| 105 | + expectIsSubgraph: true, |
| 106 | + resolveIdsAsCriterion: true, |
| 107 | + } |
| 108 | + ); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('Access with assignment', () => { |
| 113 | + assertDataflow( |
| 114 | + label('When single index is assigned, then access reads index in assignment and definition', basicCapabilities), |
| 115 | + shell, |
| 116 | + `numbers <- ${def('1', '2', '3', '4')} |
| 117 | + ${acc('numbers', 1)} <- 5 |
| 118 | + ${acc('numbers', 1)}`, |
| 119 | + (data) => emptyGraph() |
| 120 | + .readsQuery(queryAccInLine(3), queryArg(1, '1', 1), data) |
| 121 | + .readsQuery(queryAccInLine(3), queryAccInLine(2), data), |
| 122 | + { |
| 123 | + expectIsSubgraph: true, |
| 124 | + resolveIdsAsCriterion: true, |
| 125 | + } |
| 126 | + ); |
| 127 | + |
| 128 | + assertDataflow( |
| 129 | + label('When several indices are assigned, then access reads only correct index in assignment and definition', basicCapabilities), |
| 130 | + shell, |
| 131 | + `numbers <- ${def('1', '2', '3', '4')} |
| 132 | + ${acc('numbers', 1)} <- 4 |
| 133 | + ${acc('numbers', 2)} <- 3 |
| 134 | + ${acc('numbers', 3)} <- 2 |
| 135 | + ${acc('numbers', 4)} <- 1 |
| 136 | + ${acc('numbers', 1)}`, |
| 137 | + (data) => emptyGraph() |
| 138 | + .readsQuery(queryAccInLine(6), queryArg(1, '1', 1), data) |
| 139 | + .readsQuery(queryAccInLine(6), queryAccInLine(2), data), |
| 140 | + // not reads other indices |
| 141 | + { |
| 142 | + expectIsSubgraph: true, |
| 143 | + resolveIdsAsCriterion: true, |
| 144 | + } |
| 145 | + ); |
| 146 | + |
| 147 | + assertDataflow( |
| 148 | + label('When container is self-redefined with previous assignment, then indices get passed', basicCapabilities), |
| 149 | + shell, |
| 150 | + `numbers <- ${def('1', '2')} |
| 151 | + numbers <- numbers |
| 152 | + ${acc('numbers', 1)} <- 1 |
| 153 | + print(${acc('numbers', 1)})`, |
| 154 | + (data) => emptyGraph() |
| 155 | + .readsQuery({ query: Q.varInLine('numbers', 2).last() }, { target: '1@numbers' }, data) |
| 156 | + .definedByQuery( |
| 157 | + { query: Q.varInLine('numbers', 2).first() }, |
| 158 | + { query: Q.varInLine('numbers', 2).last() }, |
| 159 | + data, |
| 160 | + ), |
| 161 | + { |
| 162 | + expectIsSubgraph: true, |
| 163 | + resolveIdsAsCriterion: true, |
| 164 | + } |
| 165 | + ); |
| 166 | + }); |
| 167 | + }); |
| 168 | +})); |
0 commit comments