Ejemplo n.º 1
0
        public void ResetBitWorksForDailyMap()
        {
            // Offset             7654 3210
            const byte byte2 = 0b_0000_0100;

            // Offset             5432 1098
            const byte byte1 = 0b_0010_0000;

            // Offset             3210 9876
            const byte byte0 = 0b_1000_0000;

            var map       = new byte[] { byte0, byte1, byte2 };
            var startDate = new DateTime(2019, 1, 1);
            var frequency = StreakOccurrenceFrequency.Daily;

            // Reset a bit before the start date and get an error
            var result = StreakTypeService.SetBit(map, startDate, startDate.AddDays(-1), frequency, false, out var errorMessage);

            Assert.NotEmpty(errorMessage);  // Verify error occurred
            Assert.Same(result, map);       // Verify in-place operation
            Assert.Equal(byte0, result[0]); // Verify no changes
            Assert.Equal(byte1, result[1]); // Verify no changes
            Assert.Equal(byte2, result[2]); // Verify no changes

            // Reset a bit that is already 0
            result = StreakTypeService.SetBit(map, startDate, startDate.AddDays(0), frequency, false, out errorMessage);
            Assert.Empty(errorMessage);     // Verify no error
            Assert.Same(result, map);       // Verify in-place operation
            Assert.Equal(byte0, result[0]); // Verify no changes
            Assert.Equal(byte1, result[1]); // Verify no changes
            Assert.Equal(byte2, result[2]); // Verify no changes

            // Reset the first set bit
            result = StreakTypeService.SetBit(map, startDate, startDate.AddDays(2), frequency, false, out errorMessage);
            Assert.Empty(errorMessage);     // Verify no errors
            Assert.Same(result, map);       // Verify in-place operation
            Assert.Equal(0, result[2]);     // Verify change
            Assert.Equal(byte1, result[1]); // Verify no changes
            Assert.Equal(byte0, result[0]); // Verify no changes

            // Reset a bit beyond the array and force it to grow
            result = StreakTypeService.SetBit(map, startDate, startDate.AddDays(24), frequency, false, out errorMessage);
            Assert.Empty(errorMessage);   // Verify no errors
            Assert.NotSame(result, map);  // Verify memory allocation occurred for new array
            var newLength = 128;

            Assert.True(result.Length == newLength);    // Verify the array grew to the next multiple of 128
            Assert.Equal(0, result[newLength - 1]);     // Verify no additional changes
            Assert.Equal(byte1, result[newLength - 2]); // Verify no changes
            Assert.Equal(byte0, result[newLength - 3]); // Verify no changes

            // Verify all other bytes are unset
            for (var i = 0; i < (newLength - 3); i++)
            {
                Assert.Equal(0, result[i]);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Write the map to the view state but not to the database
        /// </summary>
        private void SaveMapState()
        {
            var streakType   = GetStreakType();
            var map          = GetTargetMap();
            var errorMessage = string.Empty;

            foreach (ListItem checkbox in cblCheckboxes.Items)
            {
                var representedDate = checkbox.Value.AsDateTime();

                if (representedDate.HasValue)
                {
                    map = StreakTypeService.SetBit(map, streakType.StartDate, representedDate.Value,
                                                   streakType.OccurrenceFrequency, checkbox.Selected, out errorMessage);
                }
            }

            ViewState[ViewStateKey.Map] = StreakTypeService.GetHexDigitStringFromMap(map);
        }