mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-24 12:35:31 +00:00
Fixed estimate cpu speed method
Added a unit test
This commit is contained in:
parent
c4c1d10a7c
commit
39f72f9dfd
2 changed files with 87 additions and 56 deletions
19
Tests/Cosmos.Core.Tests/CPUTests.cs
Normal file
19
Tests/Cosmos.Core.Tests/CPUTests.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
using NUnit.Framework;
|
||||
using Cosmos.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Cosmos.Core.Tests
|
||||
{
|
||||
[TestFixture()]
|
||||
public class CPUTests
|
||||
{
|
||||
[Test()]
|
||||
public void EstimateCPUSpeedFromNameTest()
|
||||
{
|
||||
Assert.AreEqual((long)2.8e9, CPU.EstimateCPUSpeedFromName(" Intel(R) Celeron(R) CPU 2.80GHz"));
|
||||
Assert.AreEqual((long)2.8e9, CPU.EstimateCPUSpeedFromName("Intel(R) Celeron(R) CPU 2.80GHz"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -171,6 +171,19 @@ namespace Cosmos.Core
|
|||
if (CanReadCPUID() != 0)
|
||||
{
|
||||
string s = GetCPUBrandString();
|
||||
return EstimateCPUSpeedFromName(s);
|
||||
}
|
||||
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is only public for testing purposes
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static long EstimateCPUSpeedFromName(string s)
|
||||
{
|
||||
var _words = new List<string>();
|
||||
string curr = "";
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
|
|
@ -200,24 +213,26 @@ namespace Cosmos.Core
|
|||
double value = 0;
|
||||
for (int i = 0; i < words.Length; i++)
|
||||
{
|
||||
if (words[i] == "MHz")
|
||||
var word = words[i];
|
||||
var wordEnd = word.Substring(word.Length - 3, 3);
|
||||
if (word == "MHz" || wordEnd == "MHz")
|
||||
{
|
||||
multiplier = 1e6;
|
||||
}
|
||||
else if (word == "GHz" || wordEnd == "GHz")
|
||||
{
|
||||
multiplier = 1e9;
|
||||
}
|
||||
else if (word == "THz" || wordEnd == "THz")
|
||||
{
|
||||
multiplier = 1e12;
|
||||
}
|
||||
if (value == 0)
|
||||
{
|
||||
if (Double.TryParse(word, out value) || Double.TryParse(word.Substring(0, word.Length - 3), out value))
|
||||
{
|
||||
multiplier = 10e6;
|
||||
break;
|
||||
}
|
||||
else if (words[i] == "GHz")
|
||||
{
|
||||
multiplier = 10e9;
|
||||
break;
|
||||
}
|
||||
else if (words[i] == "THz")
|
||||
{
|
||||
multiplier = 10e12;
|
||||
break;
|
||||
}
|
||||
else if (value == 0)
|
||||
{
|
||||
Double.TryParse(words[i], out value);
|
||||
}
|
||||
}
|
||||
value *= multiplier;
|
||||
|
|
@ -229,9 +244,6 @@ namespace Cosmos.Core
|
|||
return (long)value;
|
||||
}
|
||||
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get CPU cycle speed.
|
||||
/// </summary>
|
||||
|
|
|
|||
Loading…
Reference in a new issue