-
Notifications
You must be signed in to change notification settings - Fork 122
/
UnitTest.h
1325 lines (1111 loc) · 39.8 KB
/
UnitTest.h
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef UNITTEST_H_
#define UNITTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/Unit.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/UnitLabelTypes.h"
#include <boost/lexical_cast.hpp>
#include <cfloat>
#include <limits>
using namespace Mantid::Kernel;
using namespace Mantid::Kernel::Units;
// function checks if conversion within limits works reversibly
std::string convert_units_check_range(const Unit &aUnit,std::vector<double> &samples,std::vector<double> &results, double epsilon1=0)
{
std::string error_mess("");
samples.resize(4);
results.resize(4);
double tof_min = aUnit.conversionTOFMin();
double tof_max = aUnit.conversionTOFMax();
samples[0] = tof_min;
samples[1] = tof_max;
double initValMin = aUnit.singleFromTOF(tof_min);
double initValMax = aUnit.singleFromTOF(tof_max);
samples[2] = initValMin;
samples[3] = initValMax ;
results[0]= aUnit.singleToTOF(initValMin); // tof1
results[1]= aUnit.singleToTOF(initValMax); // tof2
results[2]= aUnit.singleFromTOF(results[0]); // unit 1
results[3]= aUnit.singleFromTOF(results[1]); // unit 2
auto range = aUnit.conversionRange();
double tof1=aUnit.singleToTOF(range.first);
double tof2=aUnit.singleToTOF(range.second);
bool t_increases(true);
if (tof1>tof2)
t_increases=false;
if (tof1==tof2)
{
error_mess = "conversion: "+aUnit.unitID()+ " Time range is zero (tof_left==tof_rignt)";
return error_mess;
}
if (tof1<tof_min || tof2 < tof_min)
{
error_mess = "conversion: "+aUnit.unitID()+ " min time range is smaller then minimal conversion time";
return error_mess;
}
if (tof1>tof_max*(1+epsilon1) || tof2 > tof_max*(1+epsilon1))
{
error_mess = "conversion: "+aUnit.unitID()+ "max time range is bigger then maximal conversion time";
return error_mess;
}
const size_t nSteps(100);
double step = (range.second-range.first)/nSteps;
if (step == std::numeric_limits<double>::infinity())
{
step =(DBL_MAX/nSteps)*2;
}
double t1 = aUnit.singleToTOF(range.first);
for(size_t i=1;i<=nSteps;i++)
{
double unitVal=range.first+double(i)*step;
double tofVal = aUnit.singleToTOF(unitVal);
if (t_increases)
{
if (tofVal*(1+epsilon1)< t1)
{
error_mess="conversion: "+aUnit.unitID()+" subsequent tof decreases for increasing function at step: "+boost::lexical_cast<std::string>(i);
return error_mess;
}
}
else
{
if (tofVal>t1*(1+epsilon1))
{
error_mess="conversion: "+aUnit.unitID()+" subsequent tof increases for decreasing function at step: "+boost::lexical_cast<std::string>(i);
return error_mess;
}
t1=tofVal;
}
}
return error_mess;
}
class UnitTest : public CxxTest::TestSuite
{
class UnitTester : public Unit
{
public:
UnitTester() : Unit()
{
addConversion("a", 1.1);
addConversion("b", 2.2, 0.5);
}
virtual ~UnitTester() {}
// Empty overrides of virtual methods
const std::string unitID() const {return "aUnit";}
const std::string caption() const {return "";}
const UnitLabel label () const {return UnitLabel("");}
void init() {}
virtual double singleToTOF(const double ) const { return 0; }
virtual double singleFromTOF(const double ) const { return 0; }
virtual double conversionTOFMax()const{return std::numeric_limits<double>::quiet_NaN();}
virtual double conversionTOFMin()const{return std::numeric_limits<double>::quiet_NaN();}
virtual Unit * clone() const { return new UnitTester();}
};
public:
//----------------------------------------------------------------------
// Label tests
//----------------------------------------------------------------------
void testLabel_constructor()
{
Label lbl("Temperature", "K");
TS_ASSERT_EQUALS(lbl.caption(), "Temperature");
TS_ASSERT_EQUALS(lbl.label().ascii(), "K");
}
void testLabel_unitID()
{
TS_ASSERT_EQUALS( label.unitID(), "Label" );
}
void testLabel_caption()
{
TS_ASSERT_EQUALS( label.caption(), "Quantity" );
}
void testLabel_label()
{
TS_ASSERT_EQUALS( label.label().ascii(), "" );
}
void testLabel_cast()
{
Unit *u = NULL;
TS_ASSERT_THROWS_NOTHING( u = dynamic_cast<Unit*>(&label) );
TS_ASSERT_EQUALS(u->unitID(), "Label");
}
void testLabel_setLabel()
{
label.setLabel("Temperature", "K");
TS_ASSERT_EQUALS(label.caption(), "Temperature");
TS_ASSERT_EQUALS(label.label().ascii(), "K");
}
void testLabel_limits()
{
double volatile lim_min=label.conversionTOFMin();
TS_ASSERT(lim_min!=label.conversionTOFMin());
double volatile lim_max=label.conversionTOFMax();
TS_ASSERT(lim_max!=label.conversionTOFMax());
}
//----------------------------------------------------------------------
// Base Unit class tests
//----------------------------------------------------------------------
void testUnit_quickConversion()
{
UnitTester t;
double factor;
double power;
TS_ASSERT( t.quickConversion("a",factor,power) );
TS_ASSERT_EQUALS( factor, 1.1 );
TS_ASSERT_EQUALS( power, 1.0 );
TS_ASSERT( t.quickConversion("b",factor,power) );
TS_ASSERT_EQUALS( factor, 2.2 );
TS_ASSERT_EQUALS( power, 0.5 );
TS_ASSERT( ! t.quickConversion("notThere",factor,power) );
// Test the quickConversion method that takes a Unit
Units::TOF tof;
TS_ASSERT( ! t.quickConversion(tof,factor,power) );
}
void test_clone()
{
auto unit = Empty().clone();
TS_ASSERT( dynamic_cast<Empty*>( unit ) );
delete unit;
unit = Label().clone();
TS_ASSERT( dynamic_cast<Label*>( unit ) );
delete unit;
unit = Wavelength().clone();
TS_ASSERT( dynamic_cast<Wavelength*>( unit ) );
delete unit;
unit = Energy().clone();
TS_ASSERT( dynamic_cast<Energy*>( unit ) );
delete unit;
unit = Energy_inWavenumber().clone();
TS_ASSERT( dynamic_cast<Energy_inWavenumber*>( unit ) );
delete unit;
unit = dSpacing().clone();
TS_ASSERT( dynamic_cast<dSpacing*>( unit ) );
delete unit;
unit = MomentumTransfer().clone();
TS_ASSERT( dynamic_cast<MomentumTransfer*>( unit ) );
delete unit;
unit = QSquared().clone();
TS_ASSERT( dynamic_cast<QSquared*>( unit ) );
delete unit;
unit = DeltaE().clone();
TS_ASSERT( dynamic_cast<DeltaE*>( unit ) );
delete unit;
unit = DeltaE_inWavenumber().clone();
TS_ASSERT( dynamic_cast<DeltaE_inWavenumber*>( unit ) );
delete unit;
unit = Momentum().clone();
TS_ASSERT( dynamic_cast<Momentum*>( unit ) );
delete unit;
}
//----------------------------------------------------------------------
// TOF tests
//----------------------------------------------------------------------
void testTOF_unitID()
{
TS_ASSERT_EQUALS( tof.unitID(), "TOF" );
}
void test_copy_constructor_on_concrete_type()
{
Units::TOF first;
first.initialize(1.0,1.0,1.0,2,1.0,1.0);
Units::TOF second(first);
TS_ASSERT_EQUALS(first.isInitialized(), second.isInitialized());
TS_ASSERT_EQUALS(first.unitID(), second.unitID())
TS_ASSERT_EQUALS(first.caption(), second.caption())
TS_ASSERT_EQUALS(first.label().ascii(), second.label().ascii())
TS_ASSERT_EQUALS(first.label().utf8(), second.label().utf8())
}
void test_copy_assignment_operator_on_concrete_type()
{
Units::TOF first;
first.initialize(1.0,1.0,1.0,2,1.0,1.0);
Units::TOF second;
second = first;
TS_ASSERT_EQUALS(first.isInitialized(), second.isInitialized());
TS_ASSERT_EQUALS(first.unitID(), second.unitID())
TS_ASSERT_EQUALS(first.caption(), second.caption())
TS_ASSERT_EQUALS(first.label().ascii(), second.label().ascii())
TS_ASSERT_EQUALS(first.label().utf8(), second.label().utf8())
}
void testTOF_caption()
{
TS_ASSERT_EQUALS( tof.caption(), "Time-of-flight" );
}
void testTOF_label()
{
TS_ASSERT_EQUALS( tof.label().ascii(), "microsecond" )
TS_ASSERT_EQUALS( tof.label().utf8(), L"\u03bcs" )
}
void testTOF_cast()
{
Unit *u = NULL;
TS_ASSERT_THROWS_NOTHING( u = dynamic_cast<Unit*>(&tof) );
TS_ASSERT_EQUALS(u->unitID(), "TOF");
}
void testTOF_toTOF()
{
std::vector<double> x(20, 9.9), y(20, 8.8);
std::vector<double> xx = x;
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( tof.toTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
// Check vectors are unchanged
TS_ASSERT( xx == x )
TS_ASSERT( yy == y )
}
void testTOF_fromTOF()
{
std::vector<double> x(20, 9.9), y(20, 8.8);
std::vector<double> xx = x;
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( tof.fromTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
// Check vectors are unchanged
TS_ASSERT( xx == x )
TS_ASSERT( yy == y )
}
void testTOFrange()
{
std::vector<double> sample,rezult;
std::string err_mess=convert_units_check_range(tof,sample,rezult);
TSM_ASSERT(" ERROR:"+err_mess,err_mess.size()==0);
for(size_t i=0;i<sample.size();i++)
{
TS_ASSERT_DELTA(sample[i],rezult[i],FLT_EPSILON);
}
}
//----------------------------------------------------------------------
// Wavelength tests
//----------------------------------------------------------------------
void testWavelength_unitID()
{
TS_ASSERT_EQUALS( lambda.unitID(), "Wavelength" )
}
void testWavelength_caption()
{
TS_ASSERT_EQUALS( lambda.caption(), "Wavelength" )
}
void testWavelength_label()
{
TS_ASSERT_EQUALS( lambda.label().ascii(), "Angstrom" )
TS_ASSERT_EQUALS( lambda.label().utf8(), L"\u212b" )
}
void testWavelength_cast()
{
Unit *u = NULL;
TS_ASSERT_THROWS_NOTHING( u = dynamic_cast<Unit*>(&lambda) );
TS_ASSERT_EQUALS(u->unitID(), "Wavelength");
}
void testWavelength_toTOF()
{
std::vector<double> x(1, 1.5), y(1, 1.5);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( lambda.toTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 2665.4390, 0.0001 ) // 758.3352
TS_ASSERT( yy == y )
TS_ASSERT_DELTA( lambda.convertSingleToTOF(1.5, 1.0,1.0,1.0,1,1.0,1.0), 2665.4390, 0.0001 );
}
void testWavelength_fromTOF()
{
std::vector<double> x(1, 1000.5), y(1, 1.5);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( lambda.fromTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], -5.0865, 0.0001 ) // 1.979006
TS_ASSERT( yy == y )
TS_ASSERT_DELTA( lambda.convertSingleFromTOF(1000.5, 1.0,1.0,1.0,1,1.0,1.0), -5.0865, 0.0001);
}
void testWavelength_quickConversions()
{
// Test it gives the same answer as going 'the long way'
double factor, power;
TS_ASSERT( lambda.quickConversion(energy,factor,power) )
double input = 1.1;
double result = factor * std::pow(input,power);
std::vector<double> x(1,input);
lambda.toTOF(x,x,99.0,99.0,99.0,99,99.0,99.0);
energy.fromTOF(x,x,99.0,99.0,99.0,99,99.0,99.0);
TS_ASSERT_DELTA( x[0], result, 1.0e-10 )
TS_ASSERT( lambda.quickConversion(energyk,factor,power) )
double result2 = factor * std::pow(input,power);
TS_ASSERT_EQUALS( result2/result, Mantid::PhysicalConstants::meVtoWavenumber )
std::vector<double> x2(1,input);
lambda.toTOF(x2,x2,99.0,99.0,99.0,99,99.0,99.0);
energyk.fromTOF(x2,x2,99.0,99.0,99.0,99,99.0,99.0);
TS_ASSERT_DELTA( x2[0], result2, 1.0e-10 )
}
void testWavelengthrange()
{
std::vector<double> sample,rezult;
std::string err_mess=convert_units_check_range(lambda,sample,rezult);
TSM_ASSERT(" ERROR:"+err_mess,err_mess.size()==0);
for(size_t i=0;i<sample.size();i++)
{
TSM_ASSERT_DELTA(" Failed for conversion N: "+boost::lexical_cast<std::string>(i),sample[i],rezult[i],FLT_EPSILON);
}
}
//----------------------------------------------------------------------
// Energy tests
//----------------------------------------------------------------------
void testEnergy_unitID()
{
TS_ASSERT_EQUALS( energy.unitID(), "Energy" )
}
void testEnergy_caption()
{
TS_ASSERT_EQUALS( energy.caption(), "Energy" )
}
void testEnergy_label()
{
TS_ASSERT_EQUALS( energy.label().ascii(), "meV" )
TS_ASSERT_EQUALS( energy.label().utf8(), L"meV" )
}
void testEnergy_cast()
{
Unit *u = NULL;
TS_ASSERT_THROWS_NOTHING( u = dynamic_cast<Unit*>(&energy) );
TS_ASSERT_EQUALS(u->unitID(), "Energy");
}
void testEnergy_toTOF()
{
std::vector<double> x(1, 4.0), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( energy.toTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 2286.271, 0.001 )
TS_ASSERT( yy == y )
}
void testEnergy_fromTOF()
{
std::vector<double> x(1, 4.0), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( energy.fromTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 1306759.0, 1.0 )
TS_ASSERT( yy == y )
}
void testEnergy_quickConversions()
{
// Test it gives the same answer as going 'the long way'
double factor, power;
TS_ASSERT( energy.quickConversion(energyk,factor,power) )
double input = 100.1;
double result = factor * std::pow(input,power);
TS_ASSERT_EQUALS ( result/input, Mantid::PhysicalConstants::meVtoWavenumber )
std::vector<double> x(1,input);
energy.toTOF(x,x,99.0,99.0,99.0,99,99.0,99.0);
energyk.fromTOF(x,x,99.0,99.0,99.0,99,99.0,99.0);
TS_ASSERT_DELTA( x[0], result, 1.0e-12 )
TS_ASSERT( energy.quickConversion(lambda,factor,power) )
result = factor * std::pow(input,power);
std::vector<double> x2(1,input);
energy.toTOF(x2,x2,99.0,99.0,99.0,99,99.0,99.0);
lambda.fromTOF(x2,x2,99.0,99.0,99.0,99,99.0,99.0);
TS_ASSERT_DELTA( x2[0], result, 1.0e-15 )
}
void testEnergyRange()
{
std::vector<double> sample,rezult;
std::string err_mess=convert_units_check_range(energy,sample,rezult);
TSM_ASSERT(" ERROR:"+err_mess,err_mess.size()==0);
for(size_t i=0;i<sample.size();i++)
{
if (std::fabs(sample[i])<10*FLT_EPSILON)
{
TSM_ASSERT_DELTA("Energy limits Failed for conversion N: "+boost::lexical_cast<std::string>(i),sample[i],rezult[i],10*FLT_EPSILON);
}
else
{
TSM_ASSERT_DELTA("Energy limits Failed for conversion N: "+boost::lexical_cast<std::string>(i),rezult[i]/sample[i],1.,10*FLT_EPSILON);
}
}
}
//----------------------------------------------------------------------
// Energy_inWavenumber tests
//----------------------------------------------------------------------
void testEnergy_inWavenumber_unitID()
{
TS_ASSERT_EQUALS( energyk.unitID(), "Energy_inWavenumber" )
}
void testEnergy_inWavenumber_caption()
{
TS_ASSERT_EQUALS( energyk.caption(), "Energy" )
}
void testEnergy_inWavenumber_label()
{
TS_ASSERT_EQUALS( energyk.label().ascii(), "cm^-1" )
TS_ASSERT_EQUALS( energyk.label().utf8(), L"cm\u207b\u00b9" )
}
void testEnergy_inWavenumber_cast()
{
Unit *u = NULL;
TS_ASSERT_THROWS_NOTHING( u = dynamic_cast<Unit*>(&energyk) );
TS_ASSERT_EQUALS(u->unitID(), "Energy_inWavenumber");
}
void testEnergy_inWavenumber_toTOF()
{
std::vector<double> x(1, 4.0), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( energyk.toTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 6492.989, 0.001 )
TS_ASSERT( yy == y )
}
void testEnergy_inWavenumber_fromTOF()
{
std::vector<double> x(1, 4.0), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( energyk.fromTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 10539725, 1.0 )
TS_ASSERT( yy == y )
}
void testEnergy_inWavenumber_quickConversions()
{
// Test it gives the same answer as going 'the long way'
double factor, power;
TS_ASSERT( energyk.quickConversion(energy,factor,power) )
double input = 100.1;
double result = factor * std::pow(input,power);
TS_ASSERT_EQUALS ( input/result, Mantid::PhysicalConstants::meVtoWavenumber )
std::vector<double> x(1,input);
energyk.toTOF(x,x,99.0,99.0,99.0,99,99.0,99.0);
energy.fromTOF(x,x,99.0,99.0,99.0,99,99.0,99.0);
TS_ASSERT_DELTA( x[0], result, 1.0e-14 )
TS_ASSERT( energyk.quickConversion(lambda,factor,power) )
result = factor * std::pow(input,power);
std::vector<double> x2(1,input);
energyk.toTOF(x2,x2,99.0,99.0,99.0,99,99.0,99.0);
lambda.fromTOF(x2,x2,99.0,99.0,99.0,99,99.0,99.0);
TS_ASSERT_DELTA( x2[0], result, 1.0e-15 )
}
//----------------------------------------------------------------------
// d-Spacing tests
//----------------------------------------------------------------------
void testdSpacing_unitID()
{
TS_ASSERT_EQUALS( d.unitID(), "dSpacing" )
}
void testdSpacing_caption()
{
TS_ASSERT_EQUALS( d.caption(), "d-Spacing" )
}
void testdSpacing_label()
{
TS_ASSERT_EQUALS( d.label().ascii(), "Angstrom" )
TS_ASSERT_EQUALS( d.label().utf8(), L"\u212b" )
}
void testdSpacing_cast()
{
Unit *u = NULL;
TS_ASSERT_THROWS_NOTHING( u = dynamic_cast<Unit*>(&d) );
TS_ASSERT_EQUALS(u->unitID(), "dSpacing");
}
void testdSpacing_toTOF()
{
std::vector<double> x(1, 1.0), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( d.toTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 484.7537, 0.0001 )
TS_ASSERT( yy == y )
}
void testdSpacing_fromTOF()
{
std::vector<double> x(1, 1001.1), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( d.fromTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 2.065172, 0.000001 )
TS_ASSERT( yy == y )
}
void testdSpacing_quickConversions()
{
// Test it gives the same answer as going 'the long way'
// To MomentumTransfer
double factor, power;
TS_ASSERT( d.quickConversion(q,factor,power) )
double input = 1.1;
double result = factor * std::pow(input,power);
std::vector<double> x(1,input);
d.toTOF(x,x,99.0,99.0,1.0,0,99.0,99.0);
q.fromTOF(x,x,99.0,99.0,1.0,0,99.0,99.0);
TS_ASSERT_DELTA( x[0], result, 1.0e-12 )
// To QSquared
TS_ASSERT( d.quickConversion(q2,factor,power) )
input = 1.1;
result = factor * std::pow(input,power);
x[0] = input;
d.toTOF(x,x,99.0,99.0,1.0,0,99.0,99.0);
q2.fromTOF(x,x,99.0,99.0,1.0,0,99.0,99.0);
TS_ASSERT_DELTA( x[0], result, 1.0e-12 )
}
void testdSpacingRange()
{
std::vector<double> sample,rezult;
std::string err_mess=convert_units_check_range(d,sample,rezult);
TSM_ASSERT(" ERROR:"+err_mess,err_mess.size()==0);
for(size_t i=0;i<sample.size();i++)
{
if (std::fabs(sample[i])<10*FLT_EPSILON)
{
TSM_ASSERT_DELTA("d-spacing limits Failed for conversion N: "+boost::lexical_cast<std::string>(i),sample[i],rezult[i],10*FLT_EPSILON);
}
else
{
TSM_ASSERT_DELTA("d-spacing limits Failed for conversion N: "+boost::lexical_cast<std::string>(i),rezult[i]/sample[i],1.,10*FLT_EPSILON);
}
}
}
//----------------------------------------------------------------------
// Momentum Transfer tests
//----------------------------------------------------------------------
void testQTransfer_unitID()
{
TS_ASSERT_EQUALS( q.unitID(), "MomentumTransfer" )
}
void testQTransfer_caption()
{
TS_ASSERT_EQUALS( q.caption(), "q" )
}
void testQTransfer_label()
{
TS_ASSERT_EQUALS( q.label().ascii(), "Angstrom^-1" )
TS_ASSERT_EQUALS( q.label().utf8(), L"\u212b\u207b\u00b9" )
}
void testQTransfer_cast()
{
Unit *u = NULL;
TS_ASSERT_THROWS_NOTHING( u = dynamic_cast<Unit*>(&q) );
TS_ASSERT_EQUALS(u->unitID(), "MomentumTransfer");
}
void testQTransfer_toTOF()
{
std::vector<double> x(1, 1.1), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( q.toTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 2768.9067, 0.0001 )
TS_ASSERT( yy == y )
}
void testQTransfer_fromTOF()
{
std::vector<double> x(1, 1.1), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( q.fromTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 2768.9067, 0.0001 )
TS_ASSERT( yy == y )
}
void testQTransfer_quickConversions()
{
// Test it gives the same answer as going 'the long way'
// To QSquared
double factor, power;
TS_ASSERT( q.quickConversion(q2,factor,power) )
double input = 1.1;
double result = factor * std::pow(input,power);
std::vector<double> x(1,input);
q.toTOF(x,x,99.0,99.0,99.0,99,99.0,99.0);
q2.fromTOF(x,x,99.0,99.0,99.0,99,99.0,99.0);
TS_ASSERT_DELTA( x[0], result, 1.0e-30 )
// To dSpacing
TS_ASSERT( q.quickConversion(d,factor,power) )
input = 1.1;
result = factor * std::pow(input,power);
x[0] = input;
q.toTOF(x,x,99.0,99.0,1.0,99,99.0,99.0);
d.fromTOF(x,x,99.0,99.0,1.0,99,99.0,99.0);
TS_ASSERT_DELTA( x[0], result, 1.0e-12 )
}
void testMomentumTransferRange()
{
std::vector<double> sample,rezult;
std::string err_mess=convert_units_check_range(q,sample,rezult);
TSM_ASSERT(" ERROR:"+err_mess,err_mess.size()==0);
for(size_t i=0;i<sample.size();i++)
{
if (std::fabs(sample[i])<10*FLT_EPSILON)
{
TSM_ASSERT_DELTA("Momentum transfer limits Failed for conversion N: "+boost::lexical_cast<std::string>(i),sample[i],rezult[i],10*FLT_EPSILON);
}
else
{
TSM_ASSERT_DELTA("Momentum transfer limits Failed for conversion N: "+boost::lexical_cast<std::string>(i),rezult[i]/sample[i],1.,10*FLT_EPSILON);
}
}
}
//----------------------------------------------------------------------
// Momentum Squared tests
//----------------------------------------------------------------------
void testQ2_unitID()
{
TS_ASSERT_EQUALS( q2.unitID(), "QSquared" )
}
void testQ2_caption()
{
TS_ASSERT_EQUALS( q2.caption(), "Q2" )
}
void testQ2_label()
{
TS_ASSERT_EQUALS( q2.label().ascii(), "Angstrom^-2" )
TS_ASSERT_EQUALS( q2.label().utf8(), L"\u212b\u207b\u00b2" )
}
void testQ2_cast()
{
Unit *u = NULL;
TS_ASSERT_THROWS_NOTHING( u = dynamic_cast<Unit*>(&q2) );
TS_ASSERT_EQUALS(u->unitID(), "QSquared");
}
void testQ2_toTOF()
{
std::vector<double> x(1, 4.0), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( q2.toTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 1522.899, 0.001 )
TS_ASSERT( yy == y )
}
void testQ2_fromTOF()
{
std::vector<double> x(1, 200.0), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( q2.fromTOF(x,y,1.0,1.0,1.0,1,1.0,1.0) )
TS_ASSERT_DELTA( x[0], 231.9220, 0.0001 )
TS_ASSERT( yy == y )
}
void testQ2_quickConversions()
{
// Test it gives the same answer as going 'the long way'
// To MomentumTransfer
double factor, power;
TS_ASSERT( q2.quickConversion(q,factor,power) )
double input = 1.1;
double result = factor * std::pow(input,power);
std::vector<double> x(1,input);
q2.toTOF(x,x,99.0,99.0,99.0,99,99.0,99.0);
q.fromTOF(x,x,99.0,99.0,99.0,99,99.0,99.0);
TS_ASSERT_DELTA( x[0], result, 1.0e-30 )
// To dSpacing
TS_ASSERT( q2.quickConversion(d,factor,power) )
input = 1.1;
result = factor * std::pow(input,power);
x[0] = input;
q2.toTOF(x,x,99.0,99.0,1.0,99,99.0,99.0);
d.fromTOF(x,x,99.0,99.0,1.0,99,99.0,99.0);
TS_ASSERT_DELTA( x[0], result, 1.0e-15 )
}
void testQ2Range()
{
std::vector<double> sample,rezult;
q2.initialize(1.1,1.1,99.0,0,99.0,0);
std::string err_mess=convert_units_check_range(q2,sample,rezult,-DBL_EPSILON);
TSM_ASSERT(" ERROR:"+err_mess,err_mess.size()==0);
for(size_t i=0;i<sample.size();i++)
{
if (std::fabs(sample[i])<10*FLT_EPSILON)
{
TSM_ASSERT_DELTA("Momentum transfer limits Failed for conversion N: "+boost::lexical_cast<std::string>(i),sample[i],rezult[i],10*FLT_EPSILON);
}
else
{
TSM_ASSERT_DELTA("Momentum transfer limits Failed for conversion N: "+boost::lexical_cast<std::string>(i),rezult[i]/sample[i],1.,10*FLT_EPSILON);
}
}
}
//----------------------------------------------------------------------
// Energy transfer tests
//----------------------------------------------------------------------
void testDeltaE_unitID()
{
TS_ASSERT_EQUALS( dE.unitID(), "DeltaE" )
}
void testDeltaE_caption()
{
TS_ASSERT_EQUALS( dE.caption(), "Energy transfer" )
}
void testDeltaE_label()
{
TS_ASSERT_EQUALS( dE.label().ascii(), "meV" )
TS_ASSERT_EQUALS( dE.label().utf8(), L"meV" )
}
void testDeltaE_cast()
{
Unit *u = NULL;
TS_ASSERT_THROWS_NOTHING( u = dynamic_cast<Unit*>(&dE) );
TS_ASSERT_EQUALS(u->unitID(), "DeltaE");
}
void testDeltaE_toTOF()
{
std::vector<double> x(1, 1.1), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( dE.toTOF(x,y,1.5,2.5,0.0,1,4.0,0.0) )
TS_ASSERT_DELTA( x[0], 5071.066, 0.001 )
TS_ASSERT( yy == y )
x[0] = 1.1;
TS_ASSERT_THROWS_NOTHING( dE.toTOF(x,y,1.5,2.5,0.0,2,4.0,0.0) )
TS_ASSERT_DELTA( x[0], 4376.406, 0.001 )
TS_ASSERT( yy == y )
// emode = 0
TS_ASSERT_THROWS( dE.toTOF(x,y,1.5,2.5,0.0,0,4.0,0.0), std::invalid_argument )
}
void testDeltaE_fromTOF()
{
std::vector<double> x(1, 2001.0), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( dE.fromTOF(x,y,1.5,2.5,0.0,1,4.0,0.0) )
TS_ASSERT_DELTA( x[0], -394.5692, 0.0001 )
TS_ASSERT( yy == y )
x[0] = 3001.0;
TS_ASSERT_THROWS_NOTHING( dE.fromTOF(x,y,1.5,2.5,0.0,2,4.0,0.0) )
TS_ASSERT_DELTA( x[0], 569.8397, 0.0001 )
TS_ASSERT( yy == y )
// emode = 0
TS_ASSERT_THROWS( dE.fromTOF(x,y,1.5,2.5,0.0,0,4.0,0.0), std::invalid_argument )
}
void testDERange()
{
std::vector<double> sample,rezult;
//Direct
dE.initialize(2001.0,1.0, 1.5, 1, 10., 0.0);
std::string err_mess=convert_units_check_range(dE,sample,rezult,DBL_EPSILON);
TSM_ASSERT(" ERROR:"+err_mess,err_mess.size()==0);
TSM_ASSERT_DELTA("Direct energy transfer limits Failed for conversion t_min: ",sample[0],rezult[0],10*FLT_EPSILON);
TSM_ASSERT_DELTA("Direct energy transfer limits Failed for conversion t_max: ",sample[1]/rezult[1],1.,0.05);
TSM_ASSERT_DELTA("Direct energy transfer limits Failed for conversion e_min: ",sample[2],rezult[2],10*FLT_EPSILON);
TSM_ASSERT_DELTA("Direct energy transfer limits Failed for conversion e_max: ",sample[3],rezult[3],10*FLT_EPSILON);
//Indirect
dE.initialize(2001.0,1.0, 1.5, 2, 10., 0.0);
err_mess=convert_units_check_range(dE,sample,rezult);
TSM_ASSERT(" ERROR:"+err_mess,err_mess.size()==0);
TSM_ASSERT_DELTA("Indirect energy transfer limits Failed for conversion t_min: ",sample[0],rezult[0],10*FLT_EPSILON);
TSM_ASSERT_DELTA("Indirect energy transfer limits Failed for conversion t_max: ",sample[1]/rezult[1],1.,0.05);
TSM_ASSERT_DELTA("Indirect energy transfer limits Failed for conversion e_min: ",sample[2],rezult[2],10*FLT_EPSILON);
TSM_ASSERT_DELTA("Indirect energy transfer limits Failed for conversion e_max: ",sample[3],rezult[3],10*FLT_EPSILON);
}
//----------------------------------------------------------------------
// Energy transfer in wavenumber tests
//----------------------------------------------------------------------
void testDeltaEk_unitID()
{
TS_ASSERT_EQUALS( dEk.unitID(), "DeltaE_inWavenumber" )
}
void testDeltaEk_caption()
{
TS_ASSERT_EQUALS( dEk.caption(), "Energy transfer" )
}
void testDeltaEk_label()
{
TS_ASSERT_EQUALS( dEk.label().ascii(), "cm^-1" )
TS_ASSERT_EQUALS( dEk.label().utf8(), L"cm\u207b\u00b9" )
}
void testDeltaEk_cast()
{
Unit *u = NULL;
TS_ASSERT_THROWS_NOTHING( u = dynamic_cast<Unit*>(&dEk) );
TS_ASSERT_EQUALS(u->unitID(), "DeltaE_inWavenumber");
}
void testDeltaEk_toTOF()
{
std::vector<double> x(1, 1.1), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( dEk.toTOF(x,y,1.5,2.5,0.0,1,4.0,0.0) )
TS_ASSERT_DELTA( x[0], 4622.5452, 0.01 )
TS_ASSERT( yy == y )
x[0] = 1.1;
TS_ASSERT_THROWS_NOTHING( dEk.toTOF(x,y,1.5,2.5,0.0,2,4.0,0.0) )
TS_ASSERT_DELTA( x[0], 4544.0378, 0.001 )
TS_ASSERT( yy == y )
// emode = 0
TS_ASSERT_THROWS( dEk.toTOF(x,y,1.5,2.5,0.0,0,4.0,0.0), std::invalid_argument )
}
void testDeltaEk_fromTOF()
{
std::vector<double> x(1, 2001.0), y(1, 1.0);
std::vector<double> yy = y;
TS_ASSERT_THROWS_NOTHING( dEk.fromTOF(x,y,1.5,2.5,0.0,1,4.0,0.0) )
TS_ASSERT_DELTA( x[0], -3182.416, 0.001 )
TS_ASSERT( yy == y )
x[0] = 3001.0;
TS_ASSERT_THROWS_NOTHING( dEk.fromTOF(x,y,1.5,2.5,0.0,2,4.0,0.0) )
TS_ASSERT_DELTA( x[0], 4596.068, 0.001 )
TS_ASSERT( yy == y )
// emode = 0
TS_ASSERT_THROWS( dEk.fromTOF(x,y,1.5,2.5,0.0,0,4.0,0.0), std::invalid_argument )
}
void testDE_kRange()
{
std::vector<double> sample,rezult;
//Direct
dEk.initialize(2001.0,1.0, 1.5, 1, 10., 0.0);
std::string err_mess=convert_units_check_range(dEk,sample,rezult);
TSM_ASSERT(" ERROR:"+err_mess,err_mess.size()==0);
TSM_ASSERT_DELTA("Direct energy transfer limits Failed for conversion t_min: ",sample[0],rezult[0],10*FLT_EPSILON);
TSM_ASSERT_DELTA("Direct energy transfer limits Failed for conversion t_max: ",sample[1]/rezult[1],1.,0.05);
TSM_ASSERT_DELTA("Direct energy transfer limits Failed for conversion e_min: ",sample[2],rezult[2],10*FLT_EPSILON);
TSM_ASSERT_DELTA("Direct energy transfer limits Failed for conversion e_max: ",sample[3],rezult[3],10*FLT_EPSILON);
//Indirect
dEk.initialize(2001.0,1.0, 1.5, 2, 10., 0.0);
err_mess=convert_units_check_range(dEk,sample,rezult);
TSM_ASSERT(" ERROR:"+err_mess,err_mess.size()==0);
TSM_ASSERT_DELTA("Indirect energy transfer limits Failed for conversion t_min: ",sample[0],rezult[0],10*FLT_EPSILON);
TSM_ASSERT_DELTA("Indirect energy transfer limits Failed for conversion t_max: ",sample[1]/rezult[1],1.,0.05);
TSM_ASSERT_DELTA("Indirect energy transfer limits Failed for conversion e_min: ",sample[2],rezult[2],10*FLT_EPSILON);
TSM_ASSERT_DELTA("Indirect energy transfer limits Failed for conversion e_max: ",sample[3],rezult[3],10*FLT_EPSILON);