@@ -10,6 +10,7 @@ namespace HidEngineTest.ReportDescriptorComposition
1010 using HidSpecification ;
1111 using Microsoft . VisualStudio . TestTools . UnitTesting ;
1212 using System . Collections . Generic ;
13+ using System . Linq ;
1314
1415 [ TestClass ]
1516 public class CollectionModuleTests
@@ -39,5 +40,57 @@ public void SimpleCollection()
3940 Assert . AreEqual ( HidConstants . MainItemCollectionKind . Logical , logicalCollection . Kind ) ;
4041 Assert . AreEqual ( ( array . TotalSizeInBits + variable . TotalSizeInBits ) , logicalCollection . TotalSizeInBits ) ;
4142 }
43+
44+ /// <summary>
45+ /// Modules will be combined when all fields are identical (and ReportCount == 1). This reduces the number of descriptor items.
46+ /// The combined module will have all the same fields, except, multiple UsageIds and enlarged ReportCount
47+ /// </summary>
48+ [ TestMethod ]
49+ public void OptimizedCollectionsCombineFields ( )
50+ {
51+ // Two VariableModules with ReportCount==1, preceed VariableModule with ReportCount==3
52+ // First two VariableModules will be combined.
53+ {
54+ ReportModule report = new ReportModule ( DefaultReportKind , null ) ;
55+
56+ // Using LogicalCollection, as is a simple specialization of BaseCollection.
57+ CollectionModule logicalCollection = new CollectionModule ( HidConstants . MainItemCollectionKind . Logical , report ) ;
58+
59+ DescriptorRange logicalRange = new DescriptorRange ( 0 , 10 ) ;
60+ VariableModule variable1 = new VariableModule ( HidUsageTableDefinitions . GetInstance ( ) . TryFindUsageId ( "Ordinal" , "Instance 1" ) , 1 , logicalRange , null , null , null , null , null , string . Empty , report ) ;
61+ VariableModule variable2 = new VariableModule ( HidUsageTableDefinitions . GetInstance ( ) . TryFindUsageId ( "Ordinal" , "Instance 2" ) , 1 , logicalRange , null , null , null , null , null , string . Empty , report ) ;
62+ VariableModule variable3 = new VariableModule ( HidUsageTableDefinitions . GetInstance ( ) . TryFindUsageId ( "Ordinal" , "Instance 3" ) , 3 , logicalRange , null , null , null , null , null , string . Empty , report ) ;
63+
64+ logicalCollection . Initialize ( DefaultCollectionUsage , new List < BaseModule > { variable1 , variable2 , variable3 } ) ;
65+
66+ List < ShortItem > generatedItems = logicalCollection . GenerateDescriptorItems ( true ) ;
67+
68+ uint [ ] foundReportItemCountValue = generatedItems . Where ( x => x is ReportCountItem ) . Select ( x => ( ( ReportCountItem ) x ) . Count ) . ToArray ( ) ;
69+
70+ CollectionAssert . AreEqual ( foundReportItemCountValue , new uint [ ] { 2 , 3 } ) ;
71+ }
72+
73+ // Two VariableModules with ReportCount==1, after VariableModule with ReportCount==3
74+ // Last two VariableModules will be combined.
75+ {
76+ ReportModule report = new ReportModule ( DefaultReportKind , null ) ;
77+
78+ // Using LogicalCollection, as is a simple specialization of BaseCollection.
79+ CollectionModule logicalCollection = new CollectionModule ( HidConstants . MainItemCollectionKind . Logical , report ) ;
80+
81+ DescriptorRange logicalRange = new DescriptorRange ( 0 , 10 ) ;
82+ VariableModule variable1 = new VariableModule ( HidUsageTableDefinitions . GetInstance ( ) . TryFindUsageId ( "Ordinal" , "Instance 1" ) , 1 , logicalRange , null , null , null , null , null , string . Empty , report ) ;
83+ VariableModule variable2 = new VariableModule ( HidUsageTableDefinitions . GetInstance ( ) . TryFindUsageId ( "Ordinal" , "Instance 2" ) , 1 , logicalRange , null , null , null , null , null , string . Empty , report ) ;
84+ VariableModule variable3 = new VariableModule ( HidUsageTableDefinitions . GetInstance ( ) . TryFindUsageId ( "Ordinal" , "Instance 3" ) , 3 , logicalRange , null , null , null , null , null , string . Empty , report ) ;
85+
86+ logicalCollection . Initialize ( DefaultCollectionUsage , new List < BaseModule > { variable3 , variable1 , variable2 } ) ;
87+
88+ List < ShortItem > generatedItems = logicalCollection . GenerateDescriptorItems ( true ) ;
89+
90+ uint [ ] foundReportItemCountValue = generatedItems . Where ( x => x is ReportCountItem ) . Select ( x => ( ( ReportCountItem ) x ) . Count ) . ToArray ( ) ;
91+
92+ CollectionAssert . AreEqual ( foundReportItemCountValue , new uint [ ] { 3 , 2 } ) ;
93+ }
94+ }
4295 }
4396}
0 commit comments