Skip to content

Instantly share code, notes, and snippets.

public class Inventory
{
public Dictionary<GarmentColor, int> Stock { get; set; }
public Inventory()
{
Stock = new Dictionary<GarmentColor, int>();
}
public void Add(GarmentColor color, int amount)
[Subject(typeof(Inventory), "customer swapping garment")]
public class GarmentSpecifications
{
private static Inventory _inventory;
Establish context = () =>
{
// Establish the context for our test case, set up the required objects, etc.
_inventory = new Inventory();
_inventory.Add(GarmentColor.Black, 3);
public static class TravelingSalesman
{
private static readonly Dictionary<string, Dictionary<string, int>> _connections =
new Dictionary<string, Dictionary<string, int>>();
public static void PopulateConnections()
{
if (_connections.Any())
return;
@aevitas
aevitas / lifx.cs
Created May 30, 2016 14:57
LIFX REST
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Lifx.Core.Internal;
using Lifx.Core.Protocol;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Serializers;
namespace Lifx.Core
enum WarriorTalents
{
None = 0,
Dauntless = 1,
Overpower,
SweepingStrikes,
Shockwave,
StormBolt,
DoubleTime,
@aevitas
aevitas / Read`1.cs
Last active November 16, 2016 12:56
public override unsafe T Read<T>(IntPtr address, bool isRelative = false)
{
Requires.NotEqual(address, IntPtr.Zero, nameof(address));
// We can bypass the marshal completely, unless the type we're reading has marshalling
// directives such as a [MarshalAs] attribute.
bool requiresMarshal = MarshalCache<T>.TypeRequiresMarshal;
var size = requiresMarshal ? MarshalCache<T>.Size : Unsafe.SizeOf<T>();
var buffer = ReadBytes(address, size, isRelative);
private static void BenchmarkReadsMarshal()
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(int))];
fixed (byte* b = buffer)
for (int i = 0; i < numIterations; i++)
Marshal.PtrToStructure<int>(new IntPtr(b));
}
private static void BenchmarkReadsUnsafe()
@aevitas
aevitas / PrimitiveTypeRead.cs
Last active November 25, 2016 11:45
Special type reads LocalProcessMemory
object ret = null;
var typeCode = Type.GetTypeCode(type);
// Special case simple value types:
// - Boolean
// - Byte, SByte
// - Char
// - Decimal
// - Int32, UInt32
// - Int64, UInt64
public JwtSecurityToken Generate(string userId, IEnumerable<Claim> claims)
{
Requires.NotEmpty(userId, nameof(userId));
// We're using a DateTimeOffset and later using the DateTime property thereof so we don't
// have to mess with extension methods to grab UNIX time, as DateTimeOffset has those built in.
var timeStamp = DateTimeOffset.UtcNow;
var token = new JwtSecurityToken(_options.Issuer, _options.Audience, claims, timeStamp.DateTime,
timeStamp.Add(_options.Expiration).DateTime, _options.SigningCredentials);
public class TokenProviderOptions
{
public TokenProviderOptions(string path, string issuer, string audience, TimeSpan expiration,
SigningCredentials signingCredentials)
{
Requires.NotEmpty(path, nameof(path));
Requires.NotEmpty(issuer, nameof(issuer));
Requires.NotEmpty(audience, nameof(audience));
Requires.NotNull(signingCredentials, nameof(signingCredentials));