public void ClearBit(int size, int bit) { var mask = new LogLevelBitMask(size, true, false); // check the actual size of the mask in bits int effectiveSize = (size + 31) & ~31; Assert.Equal(effectiveSize, mask.Size); // clear bit mask.ClearBit(bit); // check underlying buffer uint[] maskArray = mask.AsArray(); int clearedBitArrayIndex = bit / 32; int clearedBitIndex = bit % 32; uint[] expectedMaskArray = new uint[effectiveSize / 32]; for (int i = 0; i < expectedMaskArray.Length; i++) { if (i == clearedBitArrayIndex) { expectedMaskArray[i] = ~0u & ~(1u << clearedBitIndex); } else { expectedMaskArray[i] = ~0u; } } Assert.Equal(expectedMaskArray, maskArray); }
/// <summary> /// Calculates the logical XOR of the specified bit masks. /// </summary> /// <param name="mask1">First bit mask.</param> /// <param name="mask2">Second bit mask.</param> /// <returns> /// The resulting bit mask (has the same padding value as <paramref name="mask1"/>). /// </returns> public static LogLevelBitMask operator ^(LogLevelBitMask mask1, LogLevelBitMask mask2) { var result = new LogLevelBitMask(); int count1 = mask1.mBitField.Length; int count2 = mask2.mBitField.Length; int count = count1 > count2 ? count1 : count2; result.mBitField = count > 0 ? new uint[count] : sEmptyBitField; result.PaddingValue = mask1.PaddingValue; uint padding1 = mask1.PaddingValue ? uint.MaxValue : 0; for (int i = 0; i < count1; i++) { result.mBitField[i] = mask1.mBitField[i]; } for (int i = count1; i < count; i++) { result.mBitField[i] = padding1; } uint padding2 = mask2.PaddingValue ? uint.MaxValue : 0; for (int i = 0; i < count2; i++) { result.mBitField[i] ^= mask2.mBitField[i]; } for (int i = count2; i < count; i++) { result.mBitField[i] ^= padding2; } return(result); }
/// <summary> /// Gets a bit mask in which each bit is associated with a log level with the same id /// and expresses whether the corresponding log level is active for the specified writer. /// </summary> /// <param name="writer">Log writer to get the active log level mask for.</param> /// <returns>The requested active log level mask.</returns> public override LogLevelBitMask GetActiveLogLevelMask(LogWriter writer) { lock (Sync) { // get the first matching log writer settings LogWriterConfiguration settings = null; foreach (var configuration in File.LogWriterSettings) { if (configuration.NamePatterns.Any(x => x.Regex.IsMatch(writer.Name))) { // found match by log writer name, check tags now // - if no tags are configured => match always // - if tags are configured => match, if at least one tag matches if (!configuration.TagPatterns.Any() || configuration.TagPatterns.Any(x => writer.Tags.Any <string>(y => x.Regex.IsMatch(y)))) { settings = configuration; break; } } } if (settings != null) { LogLevelBitMask mask; // enable all log levels that are covered by the base level var level = LogLevel.GetAspect(settings.BaseLevel); // returns predefined log levels as well if (level == LogLevel.All) { mask = new LogLevelBitMask(LogLevel.MaxId + 1, true, false); } else { mask = new LogLevelBitMask(LogLevel.MaxId + 1, false, false); mask.SetBits(0, level.Id + 1); } // add log levels explicitly included foreach (string include in settings.Includes) { level = LogLevel.GetAspect(include); mask.SetBit(level.Id); } // disable log levels explicitly excluded foreach (string exclude in settings.Excludes) { level = LogLevel.GetAspect(exclude); mask.ClearBit(level.Id); } return(mask); } // no matching settings found // => disable all log levels... return(new LogLevelBitMask(0, false, false)); } }
public void IsBitCleared(int size, int bit) { var mask = new LogLevelBitMask(size, true, false); Assert.False(mask.IsBitCleared(bit)); mask.ClearBit(bit); Assert.True(mask.IsBitCleared(bit)); }
public void IsBitSet(int size, int bit) { var mask = new LogLevelBitMask(size, false, true); Assert.False(mask.IsBitSet(bit)); mask.SetBit(bit); Assert.True(mask.IsBitSet(bit)); }
/// <summary> /// Inverts the specified bit mask. /// </summary> /// <param name="mask">Bit mask to invert.</param> /// <returns>The resulting bit mask.</returns> public static LogLevelBitMask operator ~(LogLevelBitMask mask) { var result = new LogLevelBitMask(); int count = mask.mBitField.Length; result.mBitField = count > 0 ? new uint[count] : sEmptyBitField; result.PaddingValue = !mask.PaddingValue; for (int i = 0; i < count; i++) { result.mBitField[i] = ~mask.mBitField[i]; } return(result); }
[InlineData(128, 1, 126, new uint[] { 0x00000001u, 0x00000000u, 0x00000000u, 0x80000000u })] // all bits except the first and the last one public void ClearBits( int size, int fromBit, int count, uint[] expectedMaskArray) { var mask = new LogLevelBitMask(size, true, false); // check the actual size of the mask in bits int effectiveSize = (size + 31) & ~31; Assert.Equal(effectiveSize, mask.Size); // clear bit mask.ClearBits(fromBit, count); // check underlying buffer uint[] maskArray = mask.AsArray(); Assert.Equal(expectedMaskArray, maskArray); }
public void Create(int size, bool initialBitValue, bool paddingValue) { var mask = new LogLevelBitMask(size, initialBitValue, paddingValue); // check the actual size of the mask in bits int effectiveSize = (size + 31) & ~31; Assert.Equal(effectiveSize, mask.Size); // check padding value Assert.Equal(paddingValue, mask.PaddingValue); // check underlying buffer uint[] maskArray = mask.AsArray(); uint[] expectedMaskArray = new uint[effectiveSize / 32]; for (int i = 0; i < expectedMaskArray.Length; i++) { expectedMaskArray[i] = initialBitValue ? ~0u : 0u; } Assert.Equal(expectedMaskArray, maskArray); }