Skip to content

Commit 8e1ea42

Browse files
committed
Adding emit_coverage primitive
1 parent 58967e3 commit 8e1ea42

File tree

14 files changed

+189
-472
lines changed

14 files changed

+189
-472
lines changed

Src/PChecker/CheckerCore/Coverage/Code/CodeCoverage.cs

Whitespace-only changes.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Runtime.Serialization;
6+
7+
namespace PChecker.Coverage.Code
8+
{
9+
/// <summary>
10+
/// Class for storing code coverage specific information about
11+
/// a single point of coverage in the code.
12+
/// </summary>
13+
[DataContract]
14+
public class CodeCoverageInfo : IEquatable<CodeCoverageInfo>
15+
{
16+
/// <summary>
17+
/// The user-provided label for this code point.
18+
/// </summary>
19+
[DataMember]
20+
public string Label { get; }
21+
22+
/// <summary>
23+
/// The source code location information.
24+
/// </summary>
25+
[DataMember]
26+
public string CodeLocation { get; }
27+
28+
/// <summary>
29+
/// The custom payload provided by the user.
30+
/// </summary>
31+
[DataMember]
32+
public string CustomPayload { get; }
33+
34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="CodeCoverageInfo"/> class.
36+
/// </summary>
37+
/// <param name="label">User-provided label.</param>
38+
/// <param name="codeLocation">Code location identifier.</param>
39+
/// <param name="customPayload">Custom user-provided payload.</param>
40+
public CodeCoverageInfo(string label, string codeLocation, string customPayload)
41+
{
42+
Label = label ?? string.Empty;
43+
CodeLocation = codeLocation ?? string.Empty;
44+
CustomPayload = customPayload ?? string.Empty;
45+
}
46+
47+
/// <summary>
48+
/// Determines if this instance equals another instance.
49+
/// </summary>
50+
/// <param name="other">The other instance to compare with.</param>
51+
/// <returns>True if the instances are equal, false otherwise.</returns>
52+
public bool Equals(CodeCoverageInfo other)
53+
{
54+
if (ReferenceEquals(null, other)) return false;
55+
if (ReferenceEquals(this, other)) return true;
56+
57+
return string.Equals(Label, other.Label) &&
58+
string.Equals(CodeLocation, other.CodeLocation) &&
59+
string.Equals(CustomPayload, other.CustomPayload);
60+
}
61+
62+
/// <summary>
63+
/// Determines if this instance equals another object.
64+
/// </summary>
65+
/// <param name="obj">The object to compare with.</param>
66+
/// <returns>True if the object is a CodeCoverageInfo instance and is equal, false otherwise.</returns>
67+
public override bool Equals(object obj)
68+
{
69+
if (ReferenceEquals(null, obj)) return false;
70+
if (ReferenceEquals(this, obj)) return true;
71+
if (obj.GetType() != this.GetType()) return false;
72+
73+
return Equals((CodeCoverageInfo)obj);
74+
}
75+
76+
/// <summary>
77+
/// Gets the hash code for this instance.
78+
/// </summary>
79+
/// <returns>The hash code value.</returns>
80+
public override int GetHashCode()
81+
{
82+
unchecked
83+
{
84+
var hashCode = (Label != null ? Label.GetHashCode() : 0);
85+
hashCode = (hashCode * 397) ^ (CodeLocation != null ? CodeLocation.GetHashCode() : 0);
86+
hashCode = (hashCode * 397) ^ (CustomPayload != null ? CustomPayload.GetHashCode() : 0);
87+
return hashCode;
88+
}
89+
}
90+
91+
/// <summary>
92+
/// Gets a string representation of this instance.
93+
/// </summary>
94+
/// <returns>A string representation of this instance.</returns>
95+
public override string ToString()
96+
{
97+
return $"Label: '{Label}', Location: '{CodeLocation}', Payload: '{CustomPayload}'";
98+
}
99+
}
100+
}

Src/PChecker/CheckerCore/Coverage/Code/CodeCoverageReporter.cs

Whitespace-only changes.

Src/PChecker/CheckerCore/Coverage/Common/CoverageUtilities.cs

Whitespace-only changes.

Src/PChecker/CheckerCore/Coverage/CoverageUtilities.cs

Whitespace-only changes.

Src/PChecker/CheckerCore/Coverage/Event/ControlledRuntimeLogEventCoverage.cs

Whitespace-only changes.

Src/PChecker/CheckerCore/Coverage/Event/EventCoverage.cs

Whitespace-only changes.

Src/PChecker/CheckerCore/Coverage/Event/EventCoverageInfo.cs

Whitespace-only changes.

Src/PChecker/CheckerCore/Coverage/Event/EventCoverageReporter.cs

Whitespace-only changes.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Runtime.Serialization;
7+
8+
namespace PChecker.Coverage
9+
{
10+
/// <summary>
11+
/// This class maintains information about events received and sent from each state of each state machine.
12+
/// </summary>
13+
[DataContract]
14+
public class EventCoverage
15+
{
16+
/// <summary>
17+
/// Map from states to the list of events received by that state. The state id is fully qualified by
18+
/// the state machine id it belongs to.
19+
/// </summary>
20+
[DataMember]
21+
private readonly Dictionary<string, HashSet<string>> EventsReceived = new Dictionary<string, HashSet<string>>();
22+
23+
/// <summary>
24+
/// Map from states to the list of events sent by that state. The state id is fully qualified by
25+
/// the state machine id it belongs to.
26+
/// </summary>
27+
[DataMember]
28+
private readonly Dictionary<string, HashSet<string>> EventsSent = new Dictionary<string, HashSet<string>>();
29+
30+
/// <summary>
31+
/// Adds an event to the list of events received by a state.
32+
/// </summary>
33+
/// <param name="stateId">The fully qualified state ID.</param>
34+
/// <param name="eventId">The event ID to add.</param>
35+
internal void AddEventReceived(string stateId, string eventId)
36+
{
37+
CoverageUtilities.AddToHashSet(EventsReceived, stateId, eventId);
38+
}
39+
40+
/// <summary>
41+
/// Get list of events received by the given fully qualified state.
42+
/// </summary>
43+
/// <param name="stateId">The state machine qualified state name</param>
44+
public IEnumerable<string> GetEventsReceived(string stateId)
45+
{
46+
if (EventsReceived.TryGetValue(stateId, out var set))
47+
{
48+
return set;
49+
}
50+
51+
return Array.Empty<string>();
52+
}
53+
54+
/// <summary>
55+
/// Adds an event to the list of events sent by a state.
56+
/// </summary>
57+
/// <param name="stateId">The fully qualified state ID.</param>
58+
/// <param name="eventId">The event ID to add.</param>
59+
internal void AddEventSent(string stateId, string eventId)
60+
{
61+
CoverageUtilities.AddToHashSet(EventsSent, stateId, eventId);
62+
}
63+
64+
/// <summary>
65+
/// Get list of events sent by the given state.
66+
/// </summary>
67+
/// <param name="stateId">The state machine qualified state name</param>
68+
public IEnumerable<string> GetEventsSent(string stateId)
69+
{
70+
if (EventsSent.TryGetValue(stateId, out var set))
71+
{
72+
return set;
73+
}
74+
75+
return Array.Empty<string>();
76+
}
77+
78+
/// <summary>
79+
/// Merges the information from the specified coverage data.
80+
/// </summary>
81+
/// <param name="other">The coverage data to merge.</param>
82+
internal void Merge(EventCoverage other)
83+
{
84+
CoverageUtilities.MergeHashSets(EventsReceived, other.EventsReceived);
85+
CoverageUtilities.MergeHashSets(EventsSent, other.EventsSent);
86+
}
87+
}
88+
}

Src/PChecker/CheckerCore/Coverage/CoverageInfo.cs renamed to Src/PChecker/CheckerCore/Coverage/EventCoverageInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class CoverageInfo
2222
public HashSet<string> Machines { get; private set; }
2323

2424
/// <summary>
25-
/// Map from machines to set of all states states defined in that machine.
25+
/// Map from machines to set of all states defined in that machine.
2626
/// </summary>
2727
[DataMember]
2828
public Dictionary<string, HashSet<string>> MachinesToStates { get; private set; }

0 commit comments

Comments
 (0)