-
Notifications
You must be signed in to change notification settings - Fork 255
/
test.cs
143 lines (120 loc) · 3.79 KB
/
test.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Test
{
class Program
{
public class Coordinate
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Coordinate() {}
public Coordinate(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
public override bool Equals(object obj)
{
return this.Equals(obj as Coordinate);
}
public bool Equals(Coordinate p)
{
if (Object.ReferenceEquals(p, null))
{
return false;
}
if (Object.ReferenceEquals(this, p))
{
return true;
}
if (this.GetType() != p.GetType())
{
return false;
}
return X == p.X && Y == p.Y && Z == p.Z;
}
public override int GetHashCode()
{
return HashCode.Combine(X, Y, Z);
}
public static bool operator ==(Coordinate lhs, Coordinate rhs)
{
if (Object.ReferenceEquals(lhs, null))
{
if (Object.ReferenceEquals(rhs, null))
{
return true;
}
return false;
}
return lhs.Equals(rhs);
}
public static bool operator !=(Coordinate lhs, Coordinate rhs)
{
return !(lhs == rhs);
}
public override string ToString()
{
return $"Coordinate {{X: {X}, Y: {Y}, Z: {Z}}}";
}
}
public class Root
{
public List<Coordinate> Coordinates { get; set; }
}
static Coordinate Calc(string text)
{
var root = JsonConvert.DeserializeObject<Root>(text);
double x = 0;
double y = 0;
double z = 0;
int count = 0;
foreach(var c in root.Coordinates)
{
count += 1;
x += c.X;
y += c.Y;
z += c.Z;
};
return new Coordinate(x / count, y / count, z / count);
}
private static void Notify(string msg) {
try {
using (var s = new System.Net.Sockets.TcpClient("localhost", 9001)) {
var data = System.Text.Encoding.UTF8.GetBytes(msg);
s.Client.Send(data);
}
} catch {
// standalone usage
}
}
static void Main(string[] args)
{
var right = new Coordinate(2.0, 0.5, 0.25);
foreach (var v in new List<string> {
"{\"coordinates\":[{\"x\":2.0,\"y\":0.5,\"z\":0.25}]}",
"{\"coordinates\":[{\"y\":0.5,\"x\":2.0,\"z\":0.25}]}"
}) {
var left = Calc(v);
if (left != right) {
Console.Error.WriteLine($"{left} != {right}");
System.Environment.Exit(1);
}
}
var text = File.ReadAllText("/tmp/1.json");
var runtime = Type.GetType("Mono.Runtime") != null ? "Mono" : ".NET Core";
Notify($"C#/{runtime}\t{Process.GetCurrentProcess().Id}");
var results = Calc(text);
Notify("stop");
Console.WriteLine(results);
}
}
}