Commit c295204b authored by Liuhaoyang's avatar Liuhaoyang

Add AtomicInteger & tests

parent 30cca08f
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System.Threading;
namespace SkyWalking.Utils
{
public class AtomicInteger
{
private int _value;
public int Value => _value;
public AtomicInteger()
: this(0)
{
}
public AtomicInteger(int defaultValue)
{
_value = defaultValue;
}
public int Increment()
{
Interlocked.Increment(ref _value);
return _value;
}
public int Decrement()
{
Interlocked.Decrement(ref _value);
return _value;
}
public int Add(int value)
{
AddInternal(value);
return _value;
}
private void AddInternal(int value)
{
Interlocked.Add(ref _value, value);
}
public override bool Equals(object obj)
{
switch (obj)
{
case AtomicInteger atomicInteger:
return atomicInteger._value == _value;
case int value:
return value == _value;
default:
return false;
}
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
public static AtomicInteger operator +(AtomicInteger atomicInteger, int value)
{
atomicInteger.AddInternal(value);
return atomicInteger;
}
public static AtomicInteger operator +(int value, AtomicInteger atomicInteger)
{
atomicInteger.AddInternal(value);
return atomicInteger;
}
public static AtomicInteger operator -(AtomicInteger atomicInteger, int value)
{
atomicInteger.AddInternal(-value);
return atomicInteger;
}
public static AtomicInteger operator -(int value, AtomicInteger atomicInteger)
{
atomicInteger.AddInternal(-value);
return atomicInteger;
}
public static implicit operator AtomicInteger(int value)
{
return new AtomicInteger(value);
}
public static implicit operator int(AtomicInteger atomicInteger)
{
return atomicInteger._value;
}
public static bool operator ==(AtomicInteger atomicInteger, int value)
{
return atomicInteger._value == value;
}
public static bool operator !=(AtomicInteger atomicInteger, int value)
{
return !(atomicInteger == value);
}
public static bool operator ==(int value, AtomicInteger atomicInteger)
{
return atomicInteger._value == value;
}
public static bool operator !=(int value, AtomicInteger atomicInteger)
{
return !(value == atomicInteger);
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
</Project>
<ItemGroup>
<ProjectReference Include="..\..\src\SkyWalking.Abstractions\SkyWalking.Abstractions.csproj" />
<ProjectReference Include="..\..\src\SkyWalking.Core\SkyWalking.Core.csproj" />
</ItemGroup>
</Project>
\ No newline at end of file
using System;
using SkyWalking.Utils;
using Xunit;
namespace SkyWalking.Core.Tests.Utils
{
public class AtomicIntegerTests
{
[Fact]
public void Add_Test()
{
var atomicInteger = new AtomicInteger();
var result = atomicInteger.Add(2);
Assert.Equal(result, 2);
}
[Fact]
public void Increment_Test()
{
var atomicInteger = new AtomicInteger(5);
var result = atomicInteger.Increment();
Assert.Equal(result, 6);
}
[Fact]
public void Decrement_Test()
{
var atomicInteger = new AtomicInteger(5);
var result = atomicInteger.Decrement();
Assert.Equal(result, 4);
}
[Fact]
public void Operator_Add_Test()
{
var atomicInteger = new AtomicInteger(5);
var result = atomicInteger + 2;
Assert.Equal<int>(result, 7);
}
[Fact]
public void Operator_Sub_Test()
{
var atomicInteger = new AtomicInteger(5);
var result = atomicInteger - 2;
Assert.Equal<int>(result, 3);
}
[Fact]
public void Equals_Test()
{
AtomicInteger atomicInteger = 5;
Assert.True(atomicInteger.Equals(5));
}
[Fact]
public void Operator_Equals_Test()
{
AtomicInteger atomicInteger = 5;
Assert.True(atomicInteger == 5);
Assert.False(atomicInteger != 5);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment