/// <summary> /// Loads a counter example from the <paramref name="file" />. /// </summary> /// <param name="file">The path to the file the counter example should be loaded from.</param> public static CounterExample Load(string file) { Requires.NotNullOrWhitespace(file, nameof(file)); using (var reader = new BinaryReader(File.OpenRead(file), Encoding.UTF8)) { if (reader.ReadInt32() != FileHeader) throw new InvalidOperationException("The file does not contain a counter example that is compatible with this version of S#."); var endsWithException = reader.ReadBoolean(); var serializedRuntimeModel = reader.ReadBytes(reader.ReadInt32()); var modelData = RuntimeModelSerializer.LoadSerializedData(serializedRuntimeModel); foreach (var fault in modelData.ObjectTable.OfType<Fault>()) fault.Activation = (Activation)reader.ReadInt32(); var runtimeModel = new RuntimeModel(modelData); runtimeModel.UpdateFaultSets(); var metadataStream = new MemoryStream(reader.ReadBytes(reader.ReadInt32())); var formatter = new BinaryFormatter(); var slotMetadata = new StateVectorLayout((StateSlotMetadata[])formatter.Deserialize(metadataStream)); var modelMetadata = runtimeModel.StateVectorLayout; var counterExample = new byte[reader.ReadInt32()][]; var slotCount = reader.ReadInt32(); if (slotCount != runtimeModel.StateVectorSize) { throw new InvalidOperationException( $"State slot count mismatch; the instantiated model requires {runtimeModel.StateVectorSize} state slots, " + $"whereas the counter example uses {slotCount} state slots."); } if (slotMetadata.SlotCount != modelMetadata.SlotCount) { throw new InvalidOperationException( $"State slot metadata count mismatch; the instantiated model has {modelMetadata.SlotCount} state slot metadata entries, " + $"whereas the counter example has {slotMetadata.SlotCount} state slot entries."); } for (var i = 0; i < slotMetadata.SlotCount; ++i) { if (modelMetadata[i] != slotMetadata[i]) throw new StateVectorMismatchException(slotMetadata, modelMetadata); } for (var i = 0; i < counterExample.Length; ++i) { counterExample[i] = new byte[runtimeModel.StateVectorSize]; for (var j = 0; j < runtimeModel.StateVectorSize; ++j) counterExample[i][j] = reader.ReadByte(); } var replayInfo = new int[reader.ReadInt32()][]; for (var i = 0; i < replayInfo.Length; ++i) { replayInfo[i] = new int[reader.ReadInt32()]; for (var j = 0; j < replayInfo[i].Length; ++j) replayInfo[i][j] = reader.ReadInt32(); } return new CounterExample(runtimeModel, counterExample, replayInfo, endsWithException); } }