-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
2308 lines (1283 loc) · 67.9 KB
/
test.py
File metadata and controls
2308 lines (1283 loc) · 67.9 KB
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
# -*- coding: utf-8 -*-
'''
Author: Mario Massimo
Date: March 2025
'''
import pytest
import numpy as np
import os
from flock_class import Flock
SEED = int(os.getenv("SEED", 1999))
random_seed = SEED
def test_invalid_type_N_birds_in_constructor():
"""Test that the Flock class constructor raises an error when an invalid type in N_birds argument is provided.
GIVEN: An invalid type for N_birds in the class constructor
WHEN: The constructor is called to create a Flock object
THEN: A TypeError is raised
"""
with pytest.raises(TypeError,
match = 'Number of birds must be an integer number',
):
flock = Flock(N_birds = 'mille', space_length = 100, seed = random_seed)
def test_invalid_value_N_birds_in_constructor():
"""Test that the Flock class constructor raises an error when a negative number of birds is provided.
GIVEN: A negative number for N_birds in the class constructor
WHEN: The constructor is called to create a Flock object
THEN: A ValueError is raised
"""
with pytest.raises(ValueError,
match = 'Number of birds must be > 0',
):
flock = Flock(N_birds = -1, space_length = 100, seed = random_seed)
def test_invalid_type_space_length_in_constructor():
"""Test that the Flock class constructor raises an error when an invalid type in space_length argument is provided.
GIVEN: An invalid type for space_length in the class constructor
WHEN: The constructor is called to create a Flock object
THEN: A TypeError is raised
"""
with pytest.raises(TypeError,
match = 'Space length must be a floating number',
):
flock = Flock(N_birds = 200, space_length = 'mille', seed = random_seed)
def test_invalid_value_space_length_in_constructor():
"""Test that the Flock class constructor raises an error when a negative number of space length is provided.
GIVEN: A negative number for space_length in the class constructor
WHEN: The constructor is called to create a Flock object
THEN: A ValueError is raised
"""
with pytest.raises(ValueError,
match = 'Space length must be > 0',
):
flock = Flock(N_birds = 200, space_length = -1, seed = random_seed)
def test_flock_object_attributes_initialized_correctly():
"""Test that the Flock class constructor correctly inizialize object attributes.
GIVEN: Acceptable N_birds and space_length values in the class constructor
WHEN: The constructor is called to create a Flock object
THEN: object.N_birds, object.space_length are equal to the given values
"""
flock = Flock(N_birds = 234, space_length = 111.1, seed = random_seed)
assert flock.N_birds == 234
assert np.isclose(flock.space_length, 111.1)
def test_positions_shape_initialized_correctly():
"""Test that the positions attribute of the object has the correct shape after an object is created.
GIVEN: A Flock object
WHEN: I access to his attribute positions
THEN: object.positions has the right shape
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
assert np.shape(flock.positions) == (200,2)
def test_positions_values_initialized_correctly():
"""Test that the positions attribute of the object has every value in the right range.
GIVEN: A Flock object
WHEN: I access to his attribute positions
THEN: Every entry of object.positions array is within the right range
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
assert np.all((flock.positions >= 0) & (flock.positions <= 100))
def test_velocities_shape_initialized_correctly():
"""Test that the velocities attribute of the object has the correct shape after an object is created.
GIVEN: A Flock object
WHEN: I access to his attribute velocities
THEN: object.velocities has the right shape
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
assert np.shape(flock.velocities) == (200,2)
def test_init_given_positions_type_error_bool():
"""Test that the init_given_positions method raises a TypeError when a np.array with boolean values is given as input.
GIVEN: An invalid input type for input_given_positions method
WHEN: I call input_given_positions method
THEN: A TypeError is raised
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
wrong_array = np.array([True, False, False])
with pytest.raises(TypeError,
match = 'The input array must contain only numeric values',
):
flock.init_given_positions(wrong_array)
def test_init_given_positions_type_error_list():
"""Test that the init_given_positions method raises a TypeError when a list is given as input.
GIVEN: An invalid input type for input_given_positions method
WHEN: I call input_given_positions method
THEN: A TypeError is raised
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
wrong_list = [[0,0]]*200
with pytest.raises(TypeError,
match = 'The input array must be a np.ndarray',
):
flock.init_given_positions(wrong_list)
def test_init_given_positions_value_error():
"""Test that the init_given_positions method raises a ValueError when an array with invalid shape is given as input.
GIVEN: An array with invalid shape for input_given_positions method
WHEN: I call input_given_positions method
THEN: A ValueError is raised
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
wrong_array = np.zeros((199, 2))
with pytest.raises(ValueError):
flock.init_given_positions(wrong_array)
def test_init_given_positions_value_error_when_not_in_range():
"""Test that the init_given_positions method raises a ValueError when the input array has values out of the right range.
GIVEN: An array with invalid values for input_given_positions method
WHEN: I call input_given_positions method
THEN: A ValueError is raised
"""
flock = Flock(N_birds = 3, space_length = 100, seed = random_seed)
wrong_array = np.array([[1,2], [3,4], [100,-1]])
with pytest.raises(ValueError):
flock.init_given_positions(wrong_array)
def test_init_given_positions_typical_usage():
"""Test that the init_given_positions input array is equal to the object.positions attribute after calling the method
GIVEN: A valid array for init_given_positions method
WHEN: I check object.positions attribute
THEN: The two arrays are equal
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
input_array = np.zeros((200,2))
flock.init_given_positions(input_array)
assert np.allclose(input_array, flock.positions)
def test_init_given_positions_type_error_string():
"""Test that the init_given_positions method raises a TypeError when a np.array full of strings is given as input.
GIVEN: An invalid input type for input_given_positions method
WHEN: I call input_given_positions method
THEN: A TypeError is raised
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
wrong_array = np.array(['se telefonando', 'io', 'potessi dirti addio'])
with pytest.raises(TypeError,
match = 'The input array must contain only numeric values',
):
flock.init_given_velocities(wrong_array)
def test_init_given_velocities_type_error_list():
"""Test that the init_given_velocities method raises a TypeError when a list is given as input.
GIVEN: An invalid input type for input_given_velocities method
WHEN: I call input_given_velocities method
THEN: A TypeError is raised
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
wrong_list = [[0,0]]*200
with pytest.raises(TypeError,
match = 'The input array must be a np.ndarray',
):
flock.init_given_velocities(wrong_list)
def test_init_given_velocities_value_error():
"""Test that the init_given_velocities method raises a ValueError when an array with invalid shape is given as input.
GIVEN: An array with invalid shape for input_given_velocities method
WHEN: I call input_given_velocities method
THEN: A ValueError is raised
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
wrong_array = np.zeros((199, 2))
with pytest.raises(ValueError):
flock.init_given_velocities(wrong_array)
def test_init_given_velocities_value_error_when_not_in_range():
"""Test that the init_given_velocities method raises a ValueError when the input array has values out of the right range.
GIVEN: An array with invalid values for input_given_velocities method
WHEN: I call input_given_velocities method
THEN: A ValueError is raised
"""
flock = Flock(N_birds = 3, space_length = 100, seed = random_seed)
wrong_array = np.array([[0,0], [0,0], [flock.max_speed+1,0]])
with pytest.warns(UserWarning):
flock.init_given_velocities(wrong_array)
def test_init_given_velocities_typical_usage():
"""Test that the init_given_velocities input array is equal to the object.velocities attribute after calling the method.
GIVEN: A valid array for init_given_velocities method
WHEN: I check object.velocities attribute
THEN: The two arrays are equal
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
input_array = np.ones((200,2))
flock.init_given_velocities(input_array)
assert np.allclose(input_array, flock.velocities)
def test_directions_between_birds_right_shape():
"""Test that the _directions_between_birds method returns an array with the correct shape.
GIVEN: A Flock object
WHEN: I call _directions_between_birds method
THEN: The resulting array has shape (N_birds, N_birds, 2)
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
directions = flock._directions_between_birds()
assert np.shape(directions) == (200,200,2)
def test_directions_between_birds_single_bird():
"""Test that the _directions_between_birds method computed with only one bird returns an array of zeros.
GIVEN: A Flock object with a single bird
WHEN: I call _directions_between_birds method
THEN: The resulting array is an array of zeros
"""
flock = Flock(N_birds = 1, space_length = 100, seed = random_seed)
directions = flock._directions_between_birds()
zero_array = np.zeros((1,1,2))
assert np.allclose(directions, zero_array)
def test_directions_between_birds_collapsed_positions():
"""Test that the _directions_between_birds method computed when every bird is in the same position returns an array of zeros.
GIVEN: A Flock object with every bird having the same position
WHEN: I call _directions_between_birds method
THEN: The resulting array is an array of zeros
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
zero_positions = np.ones((200,2))
flock.init_given_positions(zero_positions)
directions = flock._directions_between_birds()
zero_array = np.zeros((200,200,2))
assert np.allclose(directions, zero_array)
def test_directions_between_birds_typical_usage():
"""Test that the _directions_between_birds returns the correct array when called on two birds.
GIVEN: A Flock object with two birds with known positions
WHEN: I call _directions_between_birds method
THEN: The resulting array is computed correctly
"""
flock = Flock(N_birds = 2, space_length = 100, seed = random_seed)
initial_positions = np.array([[1,2],[3,4]])
flock.init_given_positions(initial_positions)
directions = flock._directions_between_birds()
right_directions = np.array([[[ 0, 0],
[ 2, 2]],
[[-2, -2],
[ 0, 0]]])
assert np.allclose(directions, right_directions)
def test_distances_between_birds_right_shape():
"""Test that the _distances_between_birds method returns an array with the correct shape.
GIVEN: A Flock object
WHEN: I call _distances_between_birds method
THEN: The resulting array has shape (N_birds, N_birds)
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
distances = flock._distances_between_birds()
assert np.shape(distances) == (200,200)
def test_distances_between_birds_single_bird():
"""Test that the _distances_between_birds method computed with only one bird returns an array of np.inf.
GIVEN: A Flock object with a single bird
WHEN: I call _distances_between_birds method
THEN: The resulting array is an array of np.inf
"""
flock = Flock(N_birds = 1, space_length = 100, seed = random_seed)
distances = flock._distances_between_birds()
inf_array = np.ones((1,1))*np.inf
assert np.allclose(distances, inf_array)
def test_distances_between_birds_collapsed_positions():
"""Test that the _distances_between_birds method computed when every bird is in the same position returns an array of np.inf.
GIVEN: A Flock object with every bird having the same position
WHEN: I call _distances_between_birds method
THEN: The resulting array is an array of np.inf
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
zero_positions = np.ones((200,2))
flock.init_given_positions(zero_positions)
distances = flock._distances_between_birds()
inf_array = np.ones((200,200))*np.inf
assert np.allclose(distances, inf_array)
def test_distances_between_birds_typical_usage():
"""Test that the _distances_between_birds returns the correct array when called on two birds.
GIVEN: A Flock object with two birds with known positions
WHEN: I call _distances_between_birds method
THEN: The resulting array is computed correctly
"""
flock = Flock(N_birds = 2, space_length = 100, seed = random_seed)
initial_positions = np.array([[1,2],[3,4]])
flock.init_given_positions(initial_positions)
distances = flock._distances_between_birds()
right_distances = np.array([[np.inf, 2*np.sqrt(2)],
[2*np.sqrt(2), np.inf]])
assert np.allclose(distances, right_distances)
def test_directions_unitary_vectors_correct_shape():
"""Test that the _directions_unitary_vectors method returns an array with the correct shape.
GIVEN: A Flock object
WHEN: I call _directions_unitary_vectors method
THEN: The resulting array has shape (N_birds, N_birds, 2)
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
unit_distances = flock._directions_unitary_vectors()
assert np.shape(unit_distances) == (200,200,2)
def test_directions_unitary_vectors_single_bird():
"""Test that the _directions_unitary_vectors method computed with only one bird returns an array of zeros.
GIVEN: A Flock object with a single bird
WHEN: I call _directions_unitary_vectors method
THEN: The resulting array is an array of zeros
"""
flock = Flock(N_birds = 1, space_length = 100, seed = random_seed)
unit_distances = flock._directions_unitary_vectors()
zero_array = np.zeros((1,1,2))
assert np.allclose(unit_distances, zero_array)
def test_directions_unitary_vectors_collapsed_positions():
"""Test that the _directions_unitary_vectors method computed when every bird is in the same position returns an array of zeros.
GIVEN: A Flock object with every bird having the same position
WHEN: I call _directions_unitary_vectors method
THEN: The resulting array is an array of zeros
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
zero_positions = np.ones((200,2))
flock.init_given_positions(zero_positions)
unit_directions = flock._directions_unitary_vectors()
zero_array = np.zeros((200,200,2))
assert np.allclose(unit_directions, zero_array)
def test_directions_unitary_vectors_typical_usage_off_diagonal():
"""Test that the _directions_unitary_vectors returns an array which rows are normalized to one.
GIVEN: A Flock object
WHEN: I call _directions_unitary_vectors method
THEN: The resulting array rows are normalized to one
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
unit_distances = flock._directions_unitary_vectors()
diagonal_mask = np.eye(200, dtype=bool)
normalized_rows = np.linalg.norm(unit_distances[~diagonal_mask], axis=1)
correct_normalization = np.ones(200*200-200)
assert np.allclose(normalized_rows, correct_normalization)
def test_directions_unitary_vectors_typical_usage_on_diagonal():
"""Test that the _directions_unitary_vectors returns a matrix which has 0 on the diagonal.
GIVEN: A Flock object
WHEN: I call _directions_unitary_vectors method
THEN: The resulting matrix has zeros on the diagonal
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
unit_distances = flock._directions_unitary_vectors()
diagonal_mask = np.eye(200, dtype=bool)
normalized_rows = np.linalg.norm(unit_distances[diagonal_mask], axis=1)
correct_normalization = np.zeros(200)
assert np.allclose(normalized_rows, correct_normalization, atol = 1e-3)
def test_directions_unitary_vectors_typical_usage():
"""Test that the _directions_unitary_vectors returns the correct array when called on two birds.
GIVEN: A Flock object with two birds with known positions
WHEN: I call _directions_unitary_vectors method
THEN: The resulting array is computed correctly
"""
flock = Flock(N_birds = 2, space_length = 100, seed = random_seed)
initial_positions = np.array([[1,2],[3,4]])
flock.init_given_positions(initial_positions)
unit_directions = flock._directions_unitary_vectors()
right_unit_directions = np.array([[[ 0., 0.],
[ 1/np.sqrt(2), 1/np.sqrt(2)]],
[[-1/np.sqrt(2), -1/np.sqrt(2)],
[ 0., 0.]]])
assert np.allclose(unit_directions, right_unit_directions)
def test_speed_limit_factors_correct_shape():
"""Test that the _speed_limit_factors method returns an array with the correct shape.
GIVEN: A Flock object
WHEN: I call _speed_limit_factors method
THEN: The resulting array has shape (N_birds)
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
speed_limit_factors = flock._speed_limit_factors()
assert np.shape(speed_limit_factors) == (200,)
def test_speed_limit_factors_zero_velocities():
"""Test that the _speed_limit_factors method returns an array full one ones if the object.velocities are zeros.
GIVEN: A Flock object with object.velocities being full of zeros
WHEN: I call _speed_limit_factors method
THEN: The resulting array is full of ones
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
zero_array = np.zeros((200,2))
flock.init_given_velocities(zero_array)
speed_limit_factors = flock._speed_limit_factors()
correct_speed_limits = np.ones((200))
assert np.allclose(speed_limit_factors, correct_speed_limits)
def test_speed_limit_factors_typical_usage():
"""Test that the _speed_limit_factors returns the correct array when called on two birds.
GIVEN: A Flock object
WHEN: I call _speed_limit_factors method
THEN: The resulting array is computed correctly
"""
flock = Flock(N_birds = 2, space_length = 100, seed = random_seed)
speed_limit_factors = flock._speed_limit_factors()
correct_speed_limit_factors = np.linalg.norm(flock.velocities, axis=1) / flock.max_speed
correct_speed_limit_factors[correct_speed_limit_factors < 1] = 1
assert np.allclose(correct_speed_limit_factors, speed_limit_factors)
def test_visual_range_mask_typeerror():
"""Test that the _visual_range_mask method raises a TypeError when a string is given as input.
GIVEN: An invalid input type for _visual_range_mask method
WHEN: I call _visual_range_mask method
THEN: A TypeError is raised
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
with pytest.raises(TypeError,
match = 'Visual range must be a floating number',
):
flock._visual_range_mask(visual_range = 'ventimilioni')
def test_visual_range_mask_valueerror():
"""Test that the _visual_range_mask method raises a ValueError when a negative value is given as input.
GIVEN: An invalid input value for _visual_range_mask method
WHEN: I call _visual_range_mask method
THEN: A ValueError is raised
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
with pytest.raises(ValueError):
flock._visual_range_mask(visual_range = -0.4)
def test_visual_range_mask_correct_shape():
"""Test that the _visual_range_mask method returns a np.ndarray with the correct shape.
GIVEN: A Flock object
WHEN: I call _visual_range_mask method
THEN: The resulting array has shape (N_birds, N_birds)
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
mask = flock._visual_range_mask(visual_range = 20)
assert np.shape(mask) == (200,200)
def test_visual_range_mask_zero_visual_range():
"""Test that the _visual_range_mask method returns a mask full of False when visual_range is 0.
GIVEN: A Flock object
WHEN: I call _visual_range_mask method with visual_range = 0
THEN: The resulting mask is full of False
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
mask = flock._visual_range_mask(visual_range = 0)
zero_mask = np.zeros((200,200), dtype = bool)
assert np.allclose(mask, zero_mask)
def test_visual_range_mask_zero_tyipical_usage():
"""Test that the _visual_range_mask method returns a mask with True off the diagonal when birds are near each other.
GIVEN: A Flock object
WHEN: I call _visual_range_mask method having two birds near each other
THEN: The resulting mask has True off the diagonal
"""
flock = Flock(N_birds = 2, space_length = 100, seed = random_seed)
initial_positions = np.array([[1,2], [3,4]])
flock.init_given_positions(initial_positions)
diagonal_mask = np.eye(2, dtype=bool)
mask = flock._visual_range_mask(visual_range = 50)
true_array = np.array([True, True])
assert np.allclose(mask[~diagonal_mask], true_array)
def test_closest_index_correct_shape():
"""Test that the _closest_index method returns a np.ndarray with the correct shape.
GIVEN: A Flock object
WHEN: I call _closest_index method
THEN: The resulting array has shape (N_birds)
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
closest_index = flock._closest_index()
assert np.shape(closest_index) == (200,)
def test_closest_index_only_one_bird():
"""Test that the _closest_index method returns a [0] np.ndarray if there is only one bird.
GIVEN: A Flock object with one bird
WHEN: I call _closest_index method
THEN: The resulting array is equal to [0]
"""
flock = Flock(N_birds = 1, space_length = 100, seed = random_seed)
closest_index = flock._closest_index()
one_closest = np.array([0])
assert np.allclose(closest_index, one_closest)
def test_closest_index_typical_usage():
"""Test that the _closest_index method returns the expected np.ndarray given three bird with known positions.
GIVEN: A Flock object with three birds with known position
WHEN: I call _closest_index method
THEN: The resulting array has the correct values
"""
flock = Flock(N_birds = 3, space_length = 100, seed = random_seed)
initial_positions = np.array([[0,1], [0,3], [0,10]])
flock.init_given_positions(initial_positions)
closest_index = flock._closest_index()
correct_closest = np.array([1, 0, 1])
assert np.allclose(closest_index, correct_closest)
def test_num_close_non_zero_correct_shape():
"""Test that the array returned from _num_close_non_zero has the correct shape.
GIVEN: A Flock object
WHEN: I call _num_close_non_zero method
THEN: The returned array has shape (N_birds)
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
num_close_non_zero = flock._num_close_non_zero(visual_range = 20)
assert np.shape(num_close_non_zero) == (200,)
def test_num_close_non_zero_only_one_bird():
"""Test that the returned array from _num_close_non_zero is [1] if only one bird is present.
GIVEN: A Flock object with only one bird
WHEN: I call _num_close_non_zero method
THEN: The returned array is equal to [1]
"""
flock = Flock(N_birds = 1, space_length = 100, seed = random_seed)
num_close_non_zero = flock._num_close_non_zero(visual_range = 20)
one_array = np.array([1])
assert np.allclose(num_close_non_zero, one_array)
def test_num_close_non_zero_zero_visual_range():
"""Test that the _num_close_non_zero method returns an array full of ones when visual_range is 0.
GIVEN: A Flock object
WHEN: I call _num_close_non_zero method with visual_range = 0
THEN: The resulting array is full of ones
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
num_close_non_zero = flock._num_close_non_zero(visual_range = 0)
one_array = np.ones((200,))
assert np.allclose(num_close_non_zero, one_array)
def test_num_close_non_zero_typical_usage():
"""Test that the _num_close_non_zero method returns an array as expected given three birds with known positions.
GIVEN: A Flock object with three birds with known positions
WHEN: I call _num_close_non_zero method
THEN: The returned array is equal to the expected one
"""
flock = Flock(N_birds = 3, space_length = 100, seed = random_seed)
initial_positions = np.array([[1,1],[1,2],[2,2]])
flock.init_given_positions(initial_positions)
num_close_non_zero = flock._num_close_non_zero(visual_range = 20)
expected_array = np.array([2, 2, 2])
assert np.allclose(num_close_non_zero, expected_array)
def test_alignment_vector_correct_shape():
"""Test that the array returned from _alignment_vector has the correct shape.
GIVEN: A Flock object
WHEN: I call _alignment_vector method
THEN: The returned array has shape (N_birds, 2)
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
alignment_vector = flock._alignment_vector(visual_range = 20)
assert np.shape(alignment_vector) == (200,2)
def test_alignment_vector_only_one_bird():
"""Test that the returned array from _alignment_vector is [[0],[0]] if only one bird is present.
GIVEN: A Flock object with only one bird
WHEN: I call _alignment_vector method
THEN: The returned array is equal to [[0],[0]]
"""
flock = Flock(N_birds = 1, space_length = 100, seed = random_seed)
alignment_vector = flock._alignment_vector(visual_range = 20)
one_array = np.array([[0],[0]])
assert np.allclose(alignment_vector, one_array)
def test_alignment_vector_zero_visual_range():
"""Test that the _alignment_vector method returns an array full of ones when visual_range is 0.
GIVEN: A Flock object
WHEN: I call _alignment_vector method with visual_range = 0
THEN: The resulting array is full of zeros
"""
flock = Flock(N_birds = 200, space_length = 100, seed = random_seed)
alignment_vector = flock._alignment_vector(visual_range = 0)
zero_array = np.zeros((200,2))
assert np.allclose(alignment_vector, zero_array)
def test_alignment_vector_typical_usage():
"""Test that the _alignment_vector method returns an array as expected given two birds with known positions and velocities.
GIVEN: A Flock object with two birds with known positions and velocities
WHEN: I call _alignment_vector method
THEN: The returned array is equal to the expected one
"""
flock = Flock(N_birds = 2, space_length = 100, seed = random_seed)
initial_positions = np.array([[1,1],[1,2]])
initial_velocities = np.array([[1,1],[1,2]])
flock.init_given_velocities(initial_velocities)
flock.init_given_positions(initial_positions)
alignment_vector = flock._alignment_vector(visual_range = 20)
expected_array = np.array([[1., 2.],
[1., 1.]])
assert np.allclose(alignment_vector, expected_array)