forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_test.cpp
988 lines (805 loc) · 33.9 KB
/
item_test.cpp
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
#include "cata_catch.h"
#include "item.h"
#include <cmath>
#include <initializer_list>
#include <limits>
#include <memory>
#include <vector>
#include "avatar.h"
#include "avatar_action.h"
#include "calendar.h"
#include "enums.h"
#include "flag.h"
#include "game.h"
#include "item_category.h"
#include "item_factory.h"
#include "item_pocket.h"
#include "itype.h"
#include "math_defines.h"
#include "monstergenerator.h"
#include "mtype.h"
#include "player_helpers.h"
#include "ret_val.h"
#include "test_data.h"
#include "type_id.h"
#include "units.h"
#include "value_ptr.h"
static const flag_id json_flag_COLD( "COLD" );
static const flag_id json_flag_FILTHY( "FILTHY" );
static const flag_id json_flag_FIX_NEARSIGHT( "FIX_NEARSIGHT" );
static const flag_id json_flag_HOT( "HOT" );
static const item_category_id item_category_clothing( "clothing" );
static const item_category_id item_category_container( "container" );
static const item_category_id item_category_food( "food" );
static const item_category_id item_category_guns( "guns" );
static const item_category_id item_category_tools( "tools" );
static const itype_id itype_test_backpack( "test_backpack" );
static const itype_id itype_test_duffelbag( "test_duffelbag" );
static const itype_id itype_test_mp3( "test_mp3" );
static const itype_id itype_test_smart_phone( "test_smart_phone" );
static const itype_id itype_test_waterproof_bag( "test_waterproof_bag" );
static const json_character_flag json_flag_DEAF( "DEAF" );
TEST_CASE( "item_volume", "[item]" )
{
// Need to pick some item here which is count_by_charges and for which each
// charge is at least 1_ml. Battery works for now.
item i( "battery", calendar::turn_zero, item::default_charges_tag() );
REQUIRE( i.count_by_charges() );
// Would be better with Catch2 generators
const units::volume big_volume = units::from_milliliter( std::numeric_limits<int>::max() / 2 );
for( units::volume v : {
0_ml, 1_ml, i.volume(), big_volume
} ) {
INFO( "checking batteries that fit in " << v );
const int charges_that_should_fit = i.charges_per_volume( v );
i.charges = charges_that_should_fit;
CHECK( i.volume() <= v ); // this many charges should fit
i.charges++;
CHECK( i.volume() > v ); // one more charge should not fit
}
}
TEST_CASE( "simple_item_layers", "[item]" )
{
CHECK( item( "arm_warmers" ).get_layer().front() == layer_level::SKINTIGHT );
CHECK( item( "10gal_hat" ).get_layer().front() == layer_level::NORMAL );
// intentionally no waist layer check since it is obsoleted
CHECK( item( "armor_lightplate" ).get_layer().front() == layer_level::OUTER );
CHECK( item( "legrig" ).get_layer().front() == layer_level::BELTED );
}
TEST_CASE( "gun_layer", "[item]" )
{
item gun( "win70" );
item mod( "shoulder_strap" );
CHECK( gun.is_gunmod_compatible( mod ).success() );
gun.put_in( mod, item_pocket::pocket_type::MOD );
CHECK( gun.get_layer().front() == layer_level::BELTED );
CHECK( gun.get_category_of_contents().id == item_category_guns );
}
TEST_CASE( "stacking_cash_cards", "[item]" )
{
// Differently-charged cash cards should stack if neither is zero.
item cash0( "cash_card", calendar::turn_zero );
item cash1( "cash_card", calendar::turn_zero );
item cash2( "cash_card", calendar::turn_zero );
cash1.put_in( item( "money", calendar::turn_zero, 1 ), item_pocket::pocket_type::MAGAZINE );
cash2.put_in( item( "money", calendar::turn_zero, 2 ), item_pocket::pocket_type::MAGAZINE );
CHECK( !cash0.stacks_with( cash1 ) );
//CHECK( cash1.stacks_with( cash2 ) ); Enable this once cash card stacking is brought back.
}
// second minute hour day week season year
TEST_CASE( "stacking_over_time", "[item]" )
{
item A( "neccowafers" );
item B( "neccowafers" );
GIVEN( "Two items with the same birthday" ) {
REQUIRE( A.stacks_with( B ) );
WHEN( "the items are aged different numbers of seconds" ) {
A.mod_rot( A.type->comestible->spoils - 1_turns );
B.mod_rot( B.type->comestible->spoils - 3_turns );
THEN( "they don't stack" ) {
CHECK( !A.stacks_with( B ) );
}
}
WHEN( "the items are aged the same to the minute but different numbers of seconds" ) {
A.mod_rot( A.type->comestible->spoils - 5_minutes );
B.mod_rot( B.type->comestible->spoils - 5_minutes );
B.mod_rot( -5_turns );
THEN( "they stack" ) {
CHECK( A.stacks_with( B ) );
}
}
WHEN( "the items are aged a few seconds different but different minutes" ) {
A.mod_rot( A.type->comestible->spoils - 5_minutes );
B.mod_rot( B.type->comestible->spoils - 5_minutes );
B.mod_rot( 5_turns );
THEN( "they don't stack" ) {
CHECK( !A.stacks_with( B ) );
}
}
WHEN( "the items are aged the same to the hour but different numbers of minutes" ) {
A.mod_rot( A.type->comestible->spoils - 5_hours );
B.mod_rot( B.type->comestible->spoils - 5_hours );
B.mod_rot( -5_minutes );
THEN( "they stack" ) {
CHECK( A.stacks_with( B ) );
}
}
WHEN( "the items are aged a few seconds different but different hours" ) {
A.mod_rot( A.type->comestible->spoils - 5_hours );
B.mod_rot( B.type->comestible->spoils - 5_hours );
B.mod_rot( 5_turns );
THEN( "they don't stack" ) {
CHECK( !A.stacks_with( B ) );
}
}
WHEN( "the items are aged the same to the day but different numbers of seconds" ) {
A.mod_rot( A.type->comestible->spoils - 3_days );
B.mod_rot( B.type->comestible->spoils - 3_days );
B.mod_rot( -5_turns );
THEN( "they stack" ) {
CHECK( A.stacks_with( B ) );
}
}
WHEN( "the items are aged a few seconds different but different days" ) {
A.mod_rot( A.type->comestible->spoils - 3_days );
B.mod_rot( B.type->comestible->spoils - 3_days );
B.mod_rot( 5_turns );
THEN( "they don't stack" ) {
CHECK( !A.stacks_with( B ) );
}
}
WHEN( "the items are aged the same to the week but different numbers of seconds" ) {
A.mod_rot( A.type->comestible->spoils - 7_days );
B.mod_rot( B.type->comestible->spoils - 7_days );
B.mod_rot( -5_turns );
THEN( "they stack" ) {
CHECK( A.stacks_with( B ) );
}
}
WHEN( "the items are aged a few seconds different but different weeks" ) {
A.mod_rot( A.type->comestible->spoils - 7_days );
B.mod_rot( B.type->comestible->spoils - 7_days );
B.mod_rot( 5_turns );
THEN( "they don't stack" ) {
CHECK( !A.stacks_with( B ) );
}
}
WHEN( "the items are aged the same to the season but different numbers of seconds" ) {
A.mod_rot( A.type->comestible->spoils - calendar::season_length() );
B.mod_rot( B.type->comestible->spoils - calendar::season_length() );
B.mod_rot( -5_turns );
THEN( "they stack" ) {
CHECK( A.stacks_with( B ) );
}
}
WHEN( "the items are aged a few seconds different but different seasons" ) {
A.mod_rot( A.type->comestible->spoils - calendar::season_length() );
B.mod_rot( B.type->comestible->spoils - calendar::season_length() );
B.mod_rot( 5_turns );
THEN( "they don't stack" ) {
CHECK( !A.stacks_with( B ) );
}
}
}
}
TEST_CASE( "liquids_at_different_temperatures", "[item][temperature][stack][combine]" )
{
item liquid_hot( "test_liquid" );
item liquid_cold( "test_liquid" );
item liquid_filthy( "test_liquid" );
// heat_up/cold_up sets temperature of item and corresponding HOT/COLD flags
liquid_hot.heat_up(); // 60 C (333.15 K)
liquid_cold.cold_up(); // 3 C (276.15 K)
liquid_filthy.cold_up(); // 3 C (276.15 K)
liquid_filthy.set_flag( json_flag_FILTHY );
REQUIRE( units::to_kelvin( liquid_hot.temperature ) == Approx( 333.15 ) );
REQUIRE( units::to_kelvin( liquid_cold.temperature ) == Approx( 276.15 ) );
REQUIRE( liquid_hot.has_flag( json_flag_HOT ) );
REQUIRE( liquid_cold.has_flag( json_flag_COLD ) );
SECTION( "liquids at the same temperature can stack together" ) {
CHECK( liquid_cold.stacks_with( liquid_cold ) );
CHECK( liquid_hot.stacks_with( liquid_hot ) );
}
SECTION( "liquids at different temperature do not stack" ) {
// Items with different flags do not stack
CHECK_FALSE( liquid_cold.stacks_with( liquid_hot ) );
CHECK_FALSE( liquid_hot.stacks_with( liquid_cold ) );
}
SECTION( "liquids at different temperature can be combined" ) {
CHECK( liquid_cold.can_combine( liquid_hot ) );
CHECK( liquid_hot.can_combine( liquid_cold ) );
}
SECTION( "liquids with different flags can not be combined" ) {
CHECK( !liquid_cold.can_combine( liquid_filthy ) );
CHECK( !liquid_filthy.can_combine( liquid_cold ) );
}
}
static void assert_minimum_length_to_volume_ratio( const item &target )
{
if( target.type->get_id().is_null() ) {
return;
}
CAPTURE( target.type->get_id() );
CAPTURE( target.volume() );
CAPTURE( target.base_volume() );
CAPTURE( target.type->volume );
if( target.made_of( phase_id::LIQUID ) || target.is_soft() ) {
CHECK( target.length() == 0_mm );
return;
}
if( target.volume() == 0_ml ) {
CHECK( target.length() == -1_mm );
return;
}
if( target.volume() == 1_ml ) {
CHECK( target.length() >= 0_mm );
return;
}
// Minimum possible length is if the item is a sphere.
const float minimal_diameter = std::cbrt( ( 3.0 * units::to_milliliter( target.base_volume() ) ) /
( 4.0 * M_PI ) );
CHECK( units::to_millimeter( target.length() ) >= minimal_diameter * 10.0 );
}
TEST_CASE( "item_length_sanity_check", "[item]" )
{
for( const itype *type : item_controller->all() ) {
const item sample( type, calendar::turn_zero, item::solitary_tag {} );
assert_minimum_length_to_volume_ratio( sample );
}
}
TEST_CASE( "corpse_length_sanity_check", "[item]" )
{
for( const mtype &type : MonsterGenerator::generator().get_all_mtypes() ) {
const item sample = item::make_corpse( type.id );
assert_minimum_length_to_volume_ratio( sample );
}
}
static void check_spawning_in_container( const std::string &item_type )
{
item test_item{ itype_id( item_type ) };
REQUIRE( test_item.type->default_container );
item container_item = test_item.in_its_container( 1 );
CHECK( container_item.typeId() == *test_item.type->default_container );
if( container_item.is_container() ) {
CHECK( container_item.has_item_with( [&test_item]( const item & it ) {
return it.typeId() == test_item.typeId();
} ) );
} else if( test_item.is_software() ) {
REQUIRE( container_item.is_software_storage() );
const std::vector<const item *> softwares = container_item.softwares();
CHECK( !softwares.empty() );
for( const item *itm : softwares ) {
CHECK( itm->typeId() == test_item.typeId() );
}
} else {
FAIL( "Not container or software storage." );
}
}
TEST_CASE( "items_spawn_in_their_default_containers", "[item]" )
{
check_spawning_in_container( "water" );
check_spawning_in_container( "gunpowder" );
check_spawning_in_container( "nitrox" );
check_spawning_in_container( "ammonia_hydroxide" );
check_spawning_in_container( "detergent" );
check_spawning_in_container( "pale_ale" );
check_spawning_in_container( "single_malt_whiskey" );
check_spawning_in_container( "rocuronium" );
check_spawning_in_container( "chem_muriatic_acid" );
check_spawning_in_container( "chem_black_powder" );
check_spawning_in_container( "software_useless" );
}
TEST_CASE( "item_variables_round-trip_accurately", "[item]" )
{
item i( "water" );
i.set_var( "A", 17 );
CHECK( i.get_var( "A", 0 ) == 17 );
i.set_var( "B", 0.125 );
CHECK( i.get_var( "B", 0.0 ) == 0.125 );
i.set_var( "C", tripoint( 2, 3, 4 ) );
CHECK( i.get_var( "C", tripoint() ) == tripoint( 2, 3, 4 ) );
}
TEST_CASE( "water_affect_items_while_swimming_check", "[item][water][swimming]" )
{
avatar &guy = get_avatar();
clear_avatar();
GIVEN( "an item with flag WATER_DISSOLVE" ) {
REQUIRE( item( "aspirin" ).has_flag( flag_WATER_DISSOLVE ) );
WHEN( "item in hand" ) {
guy.unwield();
guy.clear_worn();
item aspirin( "aspirin" );
REQUIRE( guy.wield( aspirin ) );
THEN( "should dissolve in water" ) {
g->water_affect_items( guy );
CHECK_FALSE( guy.has_item_with_flag( flag_WATER_DISSOLVE ) );
}
}
WHEN( "item in backpack" ) {
guy.unwield();
guy.clear_worn();
item aspirin( "aspirin" );
item backpack( "backpack" );
backpack.put_in( aspirin, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wear_item( backpack ) );
THEN( "should dissolve in water" ) {
g->water_affect_items( guy );
CHECK_FALSE( guy.has_item_with_flag( flag_WATER_DISSOLVE ) );
}
}
WHEN( "item in small plastic bottle" ) {
guy.unwield();
guy.clear_worn();
item aspirin( "aspirin" );
item bottle_small( "bottle_plastic_small" );
bottle_small.put_in( aspirin, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( bottle_small ) );
THEN( "should not dissolve in water" ) {
g->water_affect_items( guy );
CHECK( guy.has_item_with_flag( flag_WATER_DISSOLVE ) );
}
}
WHEN( "item in backpack inside duffel bag" ) {
guy.unwield();
guy.clear_worn();
item aspirin( "aspirin" );
item backpack( "backpack" );
item duffelbag( "duffelbag" );
backpack.put_in( aspirin, item_pocket::pocket_type::CONTAINER );
duffelbag.put_in( backpack, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wear_item( duffelbag ) );
THEN( "should dissolve in water" ) {
g->water_affect_items( guy );
CHECK_FALSE( guy.has_item_with_flag( flag_WATER_DISSOLVE ) );
}
}
WHEN( "item in backpack inside body bag" ) {
guy.unwield();
guy.clear_worn();
item aspirin( "aspirin" );
item backpack( "backpack" );
item body_bag( "test_waterproof_bag" );
backpack.put_in( aspirin, item_pocket::pocket_type::CONTAINER );
body_bag.put_in( backpack, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( body_bag ) );
THEN( "should not dissolve in water" ) {
g->water_affect_items( guy );
CHECK( guy.has_item_with_flag( flag_WATER_DISSOLVE ) );
}
}
}
GIVEN( "an item with flag WATER_BREAK" ) {
REQUIRE( itype_test_smart_phone->has_flag( flag_WATER_BREAK ) );
WHEN( "item in hand" ) {
guy.unwield();
guy.clear_worn();
item smart_phone( itype_test_smart_phone );
REQUIRE( guy.wield( smart_phone ) );
THEN( "should be broken by water" ) {
g->water_affect_items( guy );
CHECK( guy.has_item_with_flag( flag_ITEM_BROKEN ) );
}
}
WHEN( "item in backpack" ) {
guy.unwield();
guy.clear_worn();
item smart_phone( itype_test_smart_phone );
item backpack( itype_test_backpack );
backpack.put_in( smart_phone, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( backpack ) );
THEN( "should be broken by water" ) {
g->water_affect_items( guy );
CHECK( guy.has_item_with_flag( flag_ITEM_BROKEN ) );
}
}
WHEN( "item in body bag" ) {
guy.unwield();
guy.clear_worn();
item smart_phone( itype_test_smart_phone );
item body_bag( "test_waterproof_bag" );
body_bag.put_in( smart_phone, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( body_bag ) );
THEN( "should not be broken by water" ) {
g->water_affect_items( guy );
CHECK_FALSE( guy.has_item_with_flag( flag_ITEM_BROKEN ) );
}
}
WHEN( "item in backpack inside duffel bag" ) {
guy.unwield();
guy.clear_worn();
item smart_phone( itype_test_smart_phone );
item backpack( itype_test_backpack );
item duffelbag( itype_test_duffelbag );
backpack.put_in( smart_phone, item_pocket::pocket_type::CONTAINER );
duffelbag.put_in( backpack, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( duffelbag ) );
THEN( "should be broken by water" ) {
g->water_affect_items( guy );
CHECK( guy.has_item_with_flag( flag_ITEM_BROKEN ) );
}
}
WHEN( "item in backpack inside body bag" ) {
guy.unwield();
guy.clear_worn();
item smart_phone( itype_test_smart_phone );
item backpack( itype_test_backpack );
item body_bag( itype_test_waterproof_bag );
backpack.put_in( smart_phone, item_pocket::pocket_type::CONTAINER );
body_bag.put_in( backpack, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( body_bag ) );
THEN( "should not be broken by water" ) {
g->water_affect_items( guy );
CHECK_FALSE( guy.has_item_with_flag( flag_ITEM_BROKEN ) );
}
}
}
GIVEN( "an item with flag WATER_BREAK_ACTIVE" ) {
REQUIRE( itype_test_mp3->has_flag( flag_WATER_BREAK_ACTIVE ) );
WHEN( "item in hand" ) {
guy.unwield();
guy.clear_worn();
item mp3( itype_test_mp3 );
REQUIRE( guy.wield( mp3 ) );
THEN( "should get wet from water" ) {
g->water_affect_items( guy );
CHECK( guy.get_wielded_item()->wetness > 0 );
}
}
WHEN( "item in backpack" ) {
guy.unwield();
guy.clear_worn();
item mp3( itype_test_mp3 );
item backpack( itype_test_backpack );
backpack.put_in( mp3, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( backpack ) );
THEN( "should get wet from water" ) {
g->water_affect_items( guy );
const item *test_item = guy.get_wielded_item()->all_items_top().front();
REQUIRE( test_item->typeId() == itype_test_mp3 );
CHECK( test_item->wetness > 0 );
}
}
WHEN( "item in body bag" ) {
guy.unwield();
guy.clear_worn();
item mp3( itype_test_mp3 );
item body_bag( itype_test_waterproof_bag );
body_bag.put_in( mp3, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( body_bag ) );
THEN( "should not be broken by water" ) {
g->water_affect_items( guy );
const item *test_item = guy.get_wielded_item()->all_items_top().front();
REQUIRE( test_item->typeId() == itype_test_mp3 );
CHECK( test_item->wetness == 0 );
}
}
WHEN( "item in backpack inside duffel bag" ) {
guy.unwield();
guy.clear_worn();
item mp3( itype_test_mp3 );
item backpack( itype_test_backpack );
item duffelbag( itype_test_duffelbag );
backpack.put_in( mp3, item_pocket::pocket_type::CONTAINER );
duffelbag.put_in( backpack, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( duffelbag ) );
THEN( "should get wet from water" ) {
g->water_affect_items( guy );
const item *test_item = guy.get_wielded_item()->all_items_top().front()->all_items_top().front();
REQUIRE( test_item->typeId() == itype_test_mp3 );
CHECK( test_item->wetness > 0 );
}
}
WHEN( "item in backpack inside body bag" ) {
guy.unwield();
guy.clear_worn();
item mp3( itype_test_mp3 );
item backpack( itype_test_backpack );
item body_bag( itype_test_waterproof_bag );
backpack.put_in( mp3, item_pocket::pocket_type::CONTAINER );
body_bag.put_in( backpack, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( body_bag ) );
THEN( "should not be broken by water" ) {
g->water_affect_items( guy );
const item *test_item = guy.get_wielded_item()->all_items_top().front()->all_items_top().front();
REQUIRE( test_item->typeId() == itype_test_mp3 );
CHECK( test_item->wetness == 0 );
}
}
WHEN( "item in hand" ) {
guy.unwield();
guy.clear_worn();
item mp3( itype_test_mp3 );
REQUIRE( guy.wield( mp3 ) );
THEN( "should be wet for around 8664 seconds" ) {
g->water_affect_items( guy );
CHECK( guy.get_wielded_item()->wetness == Approx( 8664 ).margin( 20 ) );
}
}
WHEN( "item in hand" ) {
guy.unwield();
guy.clear_worn();
item mp3( itype_test_mp3 );
REQUIRE( guy.wield( mp3 ) );
THEN( "gets wet five times in a row " ) {
g->water_affect_items( guy );
g->water_affect_items( guy );
g->water_affect_items( guy );
g->water_affect_items( guy );
g->water_affect_items( guy );
AND_THEN( "should be wet for around 43320 seconds" ) {
CHECK( guy.get_wielded_item()->wetness == Approx( 43320 ).margin( 100 ) );
}
}
}
}
GIVEN( "a towel" ) {
WHEN( "item in hand" ) {
guy.unwield();
guy.clear_worn();
item towel( "towel" );
REQUIRE( guy.wield( towel ) );
THEN( "should get wet in water" ) {
g->water_affect_items( guy );
CHECK( guy.has_item_with_flag( flag_WET ) );
}
}
WHEN( "wearing item" ) {
guy.unwield();
guy.clear_worn();
item towel( "towel" );
REQUIRE( guy.wear_item( towel ) );
THEN( "should get wet in water" ) {
g->water_affect_items( guy );
CHECK( guy.has_item_with_flag( flag_WET ) );
}
}
WHEN( "inside a backpack" ) {
guy.unwield();
guy.clear_worn();
item towel( "towel" );
item backpack( "backpack" );
backpack.put_in( towel, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( backpack ) );
THEN( "should get wet in water" ) {
g->water_affect_items( guy );
CHECK( guy.has_item_with_flag( flag_WET ) );
}
}
WHEN( "inside a body bag" ) {
guy.unwield();
guy.clear_worn();
item towel( "towel" );
item body_bag( "test_waterproof_bag" );
body_bag.put_in( towel, item_pocket::pocket_type::CONTAINER );
REQUIRE( guy.wield( body_bag ) );
THEN( "should not get wet in water" ) {
g->water_affect_items( guy );
CHECK_FALSE( guy.has_item_with_flag( flag_WET ) );
}
}
}
}
static float max_density_for_mats( const std::map<material_id, int> &mats, float total_size )
{
REQUIRE( !mats.empty() );
float max_density = 0.f;
for( const auto &m : mats ) {
max_density += m.first->density() * ( m.second / total_size );
}
return max_density;
}
static float item_density( const item &target )
{
return static_cast<float>( to_gram( target.weight() ) ) / static_cast<float>( to_milliliter(
target.volume() ) );
}
static bool assert_maximum_density_for_material( const item &target )
{
if( to_milliliter( target.volume() ) == 0 ) {
return false;
}
const std::map<material_id, int> &mats = target.made_of();
if( !mats.empty() && test_data::known_bad.count( target.typeId() ) == 0 ) {
const float max_density = max_density_for_mats( mats, target.type->mat_portion_total );
CAPTURE( target.typeId(), target.weight(), target.volume() );
CHECK( item_density( target ) <= max_density );
return item_density( target ) > max_density;
}
// fallback return
return false;
}
TEST_CASE( "item_material_density_sanity_check", "[item]" )
{
std::vector<const itype *> all_items = item_controller->all();
// only allow so many failures before stopping
int number_of_failures = 0;
for( const itype *type : all_items ) {
const item sample( type, calendar::turn_zero, item::solitary_tag{} );
if( assert_maximum_density_for_material( sample ) ) {
number_of_failures++;
if( number_of_failures > 20 ) {
break;
}
}
}
}
TEST_CASE( "item_material_density_blacklist_is_pruned", "[item]" )
{
for( const itype_id &bad : test_data::known_bad ) {
if( !bad.is_valid() ) {
continue;
}
const item target( bad, calendar::turn_zero, item::solitary_tag{} );
if( to_milliliter( target.volume() ) == 0 ) {
continue;
}
const std::map<material_id, int> &mats = target.made_of();
if( !mats.empty() ) {
INFO( string_format( "%s had its density fixed, remove it from the list in data/mods/TEST_DATA/known_bad_density.json",
bad.str() ) );
const float max_density = max_density_for_mats( mats, bad->mat_portion_total );
CHECK( item_density( target ) > max_density );
}
}
}
TEST_CASE( "armor_entry_consolidate_check", "[item][armor]" )
{
item test_consolidate( "test_consolidate" );
//check this item has a single armor entry, not 3 like is written in the json explicitly
CHECK( test_consolidate.find_armor_data()->sub_data.size() == 1 );
}
TEST_CASE( "module_inheritance", "[item][armor]" )
{
avatar &guy = get_avatar();
clear_avatar();
guy.set_body();
guy.clear_mutations();
guy.clear_worn();
item test_exo( "test_modular_exosuit" );
item test_module( "test_exo_lense_module" );
test_exo.force_insert_item( test_module, item_pocket::pocket_type::CONTAINER );
guy.worn.wear_item( guy, test_exo, false, false, false );
CHECK( guy.worn.worn_with_flag( json_flag_FIX_NEARSIGHT ) );
clear_avatar();
item miner_hat( "miner_hat" );
item ear_muffs( "attachable_ear_muffs" );
REQUIRE( miner_hat.put_in( ear_muffs, item_pocket::pocket_type::CONTAINER ).success() );
REQUIRE( !miner_hat.has_flag( json_flag_DEAF ) );
guy.wear_item( miner_hat );
item_location worn_hat = guy.worn.top_items_loc( guy ).front();
item_location worn_muffs( worn_hat, &worn_hat->only_item() );
avatar_action::use_item( guy, worn_muffs, "transform" );
CHECK( worn_hat->has_flag( json_flag_DEAF ) );
}
TEST_CASE( "rigid_armor_compliance", "[item][armor]" )
{
avatar &guy = get_avatar();
clear_avatar();
// check if you can swap a rigid armor
item test_armguard( "test_armguard" );
REQUIRE( guy.wield( test_armguard ) );
REQUIRE( guy.wear( guy.used_weapon(), false ) );
CHECK( guy.worn.top_items_loc( guy ).front().get_item()->get_side() == side::LEFT );
guy.change_side( *guy.worn.top_items_loc( guy ).front().get_item() );
CHECK( guy.worn.top_items_loc( guy ).front().get_item()->get_side() == side::RIGHT );
// check if you can't wear 3 rigid armors
clear_avatar();
item first_test_armguard( "test_armguard" );
REQUIRE( guy.wield( first_test_armguard ) );
REQUIRE( guy.wear( guy.used_weapon(), false ) );
item second_test_armguard( "test_armguard" );
REQUIRE( guy.wield( second_test_armguard ) );
REQUIRE( guy.wear( guy.used_weapon(), false ) );
item third_test_armguard( "test_armguard" );
REQUIRE( guy.wield( third_test_armguard ) );
REQUIRE( !guy.wear( guy.used_weapon(), false ) );
}
TEST_CASE( "rigid_splint_compliance", "[item][armor]" )
{
avatar &guy = get_avatar();
clear_avatar();
item test_armguard( "test_armguard" );
item second_test_armguard( "test_armguard" );
item splint( "arm_splint" );
item second_splint( "arm_splint" );
item third_splint( "arm_splint" );
// check if you can wear a splint
clear_avatar();
guy.set_part_hp_cur( bodypart_id( "arm_r" ), 0 );
REQUIRE( guy.wield( splint ) );
REQUIRE( guy.wear( guy.used_weapon(), false ) );
// check if you cannot wear a splint if something rigid is on that arm
clear_avatar();
REQUIRE( guy.wield( test_armguard ) );
guy.set_part_hp_cur( bodypart_id( "arm_r" ), 0 );
REQUIRE( guy.wear( guy.used_weapon(), false ) );
CHECK( guy.worn.top_items_loc( guy ).front().get_item()->get_side() == side::LEFT );
// swap side to the broken arm side
guy.change_side( *guy.worn.top_items_loc( guy ).front().get_item() );
// should fail to wear
REQUIRE( guy.wield( second_splint ) );
REQUIRE( !guy.wear( guy.used_weapon(), false ) );
// check if you can wear a splint if nothing rigid is on that arm
clear_avatar();
REQUIRE( guy.wield( second_test_armguard ) );
guy.set_part_hp_cur( bodypart_id( "arm_r" ), 0 );
REQUIRE( guy.wear( guy.used_weapon(), false ) );
CHECK( guy.worn.top_items_loc( guy ).front().get_item()->get_side() == side::LEFT );
// should be able to wear the arm is open
REQUIRE( guy.wield( third_splint ) );
REQUIRE( guy.wear( guy.used_weapon(), false ) );
}
TEST_CASE( "item_single_type_contents", "[item]" )
{
item walnut( "walnut" );
item nail( "nail" );
item bag( "bag_plastic" );
REQUIRE( bag.get_category_of_contents().id == item_category_container );
int const num = GENERATE( 1, 2 );
bool ret = true;
for( int i = 0; i < num; i++ ) {
ret &= bag.put_in( walnut, item_pocket::pocket_type::CONTAINER ).success();
}
REQUIRE( ret );
CAPTURE( num, bag.display_name() );
CHECK( bag.get_category_of_contents() == *item_category_food );
REQUIRE( nail.get_category_of_contents().id != walnut.get_category_of_contents().id );
REQUIRE( bag.put_in( nail, item_pocket::pocket_type::CONTAINER ).success() );
CHECK( bag.get_category_of_contents().id == item_category_container );
SECTION( "clothing" ) {
item jeans( "jeans" );
REQUIRE( jeans.get_category_of_contents().id == item_category_clothing );
REQUIRE( walnut.get_category_of_contents().id == item_category_food );
REQUIRE( jeans.put_in( walnut, item_pocket::pocket_type::CONTAINER ).success() );
CHECK( jeans.get_category_of_contents().id == item_category_clothing );
}
SECTION( "software" ) {
item usb_drive( "usb_drive" );
item software_hacking( "software_hacking" );
REQUIRE( usb_drive.get_category_of_contents().id == item_category_tools );
REQUIRE( usb_drive.put_in( software_hacking, item_pocket::pocket_type::SOFTWARE ).success() );
CHECK( usb_drive.get_category_of_contents().id == item_category_tools );
}
}
TEST_CASE( "item_nested_contents", "[item]" )
{
item walnut( "walnut" );
item outer_bag( "bag_plastic" );
item inner_bag1( "bag_plastic" );
item inner_bag2( "bag_plastic" );
REQUIRE( inner_bag1.put_in( walnut, item_pocket::pocket_type::CONTAINER ).success() );
REQUIRE( inner_bag1.put_in( walnut, item_pocket::pocket_type::CONTAINER ).success() );
CHECK( inner_bag1.get_category_of_contents().id == item_category_food );
REQUIRE( inner_bag2.put_in( walnut, item_pocket::pocket_type::CONTAINER ).success() );
CHECK( inner_bag2.get_category_of_contents().id == item_category_food );
REQUIRE( outer_bag.put_in( inner_bag1, item_pocket::pocket_type::CONTAINER ).success() );
REQUIRE( outer_bag.put_in( inner_bag2, item_pocket::pocket_type::CONTAINER ).success() );
CAPTURE( outer_bag.display_name() );
// outer_bag
// inner_bag1
// walnut
// walnut
// inner_bag2
// walnut
CHECK( outer_bag.get_category_of_contents().id == item_category_food );
}
TEST_CASE( "item_rotten_contents", "[item]" )
{
item wrapper( "wrapper" );
REQUIRE( wrapper.get_category_of_contents().id == item_category_container );
item butter_rotten( "butter" );
butter_rotten.set_relative_rot( 1.01 );
REQUIRE( wrapper.put_in( butter_rotten, item_pocket::pocket_type::CONTAINER ).success() );
REQUIRE( wrapper.put_in( butter_rotten, item_pocket::pocket_type::CONTAINER ).success() );
CAPTURE( wrapper.display_name() );
CHECK( wrapper.get_category_of_contents().id == item_category_food );
item butter( "butter" );
butter.set_relative_rot( 0.5 );
REQUIRE( wrapper.put_in( butter, item_pocket::pocket_type::CONTAINER ).success() );
CAPTURE( wrapper.display_name() );
// wrapper
// butter (rotten)
// butter (rotten)
// butter
CHECK( wrapper.get_category_of_contents().id == item_category_food );
}