Skip to content

Commit 6d7f303

Browse files
committed
added tests for invalid combinations of variablenames and indextable
1 parent 658cf92 commit 6d7f303

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/inherent_discrete_grid.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ function _build_lookup_table(Rs, indextable, variablenames, ::Val{D}) where D
5252
for (pos_in_site, qindex) in pairs(quanticsindices)
5353
variablename, bitnumber = qindex
5454
var_idx = findfirst(==(variablename), variablenames)
55+
if isnothing(var_idx)
56+
throw(ArgumentError("Unknown variable $variablename specified on site $site_idx. Known variable names are $variablenames."))
57+
end
5558
lookup_table[var_idx][bitnumber] = (site_idx, pos_in_site)
5659
end
5760
end

test/newgrid/inherentdiscretegrid.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ end
425425
variablenames = (:x, :y)
426426
indextable = [
427427
[(:x, 1)], # site 1: x_1
428-
[(:x, 2)], # site 2: x_2
428+
[(:x, 2)], # site 2: x_2
429429
[(:y, 1)], # site 3: y_1
430430
[(:x, 3), (:y, 2)] # site 4: x_3, y_2
431431
]
@@ -578,7 +578,7 @@ end
578578
variablenames = (:a, :b)
579579
indextable = [
580580
[(:a, 1)], # site 1: a_1
581-
[(:a, 2)], # site 2: a_2
581+
[(:a, 2)], # site 2: a_2
582582
[(:a, 3)], # site 3: a_3
583583
[(:b, 1)], # site 4: b_1
584584
[(:b, 2)] # site 5: b_2
@@ -653,7 +653,7 @@ end
653653
quantics = QuanticsGrids.grididx_to_quantics(grid, test_grididx)
654654
@test length(quantics) == 4
655655
@test all(1 .<= quantics[1] .<= 25) # site 1
656-
@test all(1 .<= quantics[2] .<= 25) # site 2
656+
@test all(1 .<= quantics[2] .<= 25) # site 2
657657
@test all(1 .<= quantics[3] .<= 5) # site 3
658658
@test all(1 .<= quantics[4] .<= 125) # site 4
659659
@test QuanticsGrids.quantics_to_grididx(grid, quantics) == test_grididx
@@ -728,8 +728,25 @@ end
728728
[(:z, 1)] # z is not in variablenames
729729
]
730730

731-
@test_throws AssertionError InherentDiscreteGrid(variablenames, invalid_indextable_unknown;
731+
@test_throws AssertionError InherentDiscreteGrid(
732+
variablenames, invalid_indextable_unknown;
732733
origin=(1, 1), step=(1, 1))
734+
735+
invalid_indextable_missing_index = [
736+
[(:x, 1), (:y, 1)],
737+
[(:x, 3), (:y, 2)]
738+
]
739+
740+
@test_throws AssertionError InherentDiscreteGrid(
741+
variablenames, invalid_indextable_missing_index)
742+
743+
invalid_indextable_repeated_index = [
744+
[(:x, 1), (:y, 1)],
745+
[(:x, 1), (:y, 2)]
746+
]
747+
748+
@test_throws AssertionError InherentDiscreteGrid(
749+
variablenames, invalid_indextable_repeated_index)
733750
end
734751

735752
@testitem "InherentDiscreteGrid constructor with variablenames and Rs" begin
@@ -880,7 +897,7 @@ end
880897
grid_step_tuple = InherentDiscreteGrid(Rs; step=(2, 5))
881898
@test grid_step_tuple.step == (2, 5)
882899

883-
# Test origin can be specified as single value or tuple
900+
# Test origin can be specified as single value or tuple
884901
grid_origin_single = InherentDiscreteGrid(Rs; origin=7)
885902
@test grid_origin_single.origin == (7, 7) # should broadcast
886903

@@ -974,7 +991,7 @@ end
974991
@test grid_mixed.origin == origin_multi
975992
@test grid_mixed.step == step_multi
976993

977-
# Multi-R with single origin/step values
994+
# Multi-R with single origin/step values
978995
Rs_multi = (2, 4, 3)
979996
origin_single = 7
980997
step_single = 2

0 commit comments

Comments
 (0)